copy function in batch script does not work









up vote
0
down vote

favorite












I am writing a batch script where I am writing a set of instruction inside function as I want to call it many time so want to reuse it.For me it works when i write outside function but inside function it never works . Below the code which I have used .



@echo off

set _prefs="%APPDATA%testtestBrowserProfileprefs.js"
set _prefs_notes="%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y %_prefs_notes% %_prefs_notes%.copy1 > nul

CALL :AMEND_PREFJS %_prefs_notes%
EXIT /B




:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y %~1 %~1.copy > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end

set %~1=
EXIT /B 0
goto end

:prefs_not_found
rem set error level?
echo "file does not exist -- %_prefs_notes%"

:end
set _prefs=









share|improve this question























  • I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
    – Squashman
    Nov 9 at 18:37











  • sorry I forgot the quotes while trying to play with it .It works with quotes
    – Rajesh
    Nov 9 at 18:43






  • 1




    So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
    – Squashman
    Nov 9 at 18:45











  • Well there is no error i donot see any error only the copy does not get created
    – Rajesh
    Nov 9 at 18:51










  • find str throws error though
    – Rajesh
    Nov 9 at 18:51














up vote
0
down vote

favorite












I am writing a batch script where I am writing a set of instruction inside function as I want to call it many time so want to reuse it.For me it works when i write outside function but inside function it never works . Below the code which I have used .



@echo off

set _prefs="%APPDATA%testtestBrowserProfileprefs.js"
set _prefs_notes="%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y %_prefs_notes% %_prefs_notes%.copy1 > nul

CALL :AMEND_PREFJS %_prefs_notes%
EXIT /B




:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y %~1 %~1.copy > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end

set %~1=
EXIT /B 0
goto end

:prefs_not_found
rem set error level?
echo "file does not exist -- %_prefs_notes%"

:end
set _prefs=









share|improve this question























  • I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
    – Squashman
    Nov 9 at 18:37











  • sorry I forgot the quotes while trying to play with it .It works with quotes
    – Rajesh
    Nov 9 at 18:43






  • 1




    So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
    – Squashman
    Nov 9 at 18:45











  • Well there is no error i donot see any error only the copy does not get created
    – Rajesh
    Nov 9 at 18:51










  • find str throws error though
    – Rajesh
    Nov 9 at 18:51












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am writing a batch script where I am writing a set of instruction inside function as I want to call it many time so want to reuse it.For me it works when i write outside function but inside function it never works . Below the code which I have used .



@echo off

set _prefs="%APPDATA%testtestBrowserProfileprefs.js"
set _prefs_notes="%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y %_prefs_notes% %_prefs_notes%.copy1 > nul

CALL :AMEND_PREFJS %_prefs_notes%
EXIT /B




:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y %~1 %~1.copy > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end

set %~1=
EXIT /B 0
goto end

:prefs_not_found
rem set error level?
echo "file does not exist -- %_prefs_notes%"

:end
set _prefs=









share|improve this question















I am writing a batch script where I am writing a set of instruction inside function as I want to call it many time so want to reuse it.For me it works when i write outside function but inside function it never works . Below the code which I have used .



@echo off

set _prefs="%APPDATA%testtestBrowserProfileprefs.js"
set _prefs_notes="%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y %_prefs_notes% %_prefs_notes%.copy1 > nul

CALL :AMEND_PREFJS %_prefs_notes%
EXIT /B




:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y %~1 %~1.copy > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end

set %~1=
EXIT /B 0
goto end

:prefs_not_found
rem set error level?
echo "file does not exist -- %_prefs_notes%"

:end
set _prefs=






batch-file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 18:42

























asked Nov 9 at 18:21









Rajesh

88141538




88141538











  • I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
    – Squashman
    Nov 9 at 18:37











  • sorry I forgot the quotes while trying to play with it .It works with quotes
    – Rajesh
    Nov 9 at 18:43






  • 1




    So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
    – Squashman
    Nov 9 at 18:45











  • Well there is no error i donot see any error only the copy does not get created
    – Rajesh
    Nov 9 at 18:51










  • find str throws error though
    – Rajesh
    Nov 9 at 18:51
















  • I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
    – Squashman
    Nov 9 at 18:37











  • sorry I forgot the quotes while trying to play with it .It works with quotes
    – Rajesh
    Nov 9 at 18:43






  • 1




    So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
    – Squashman
    Nov 9 at 18:45











  • Well there is no error i donot see any error only the copy does not get created
    – Rajesh
    Nov 9 at 18:51










  • find str throws error though
    – Rajesh
    Nov 9 at 18:51















I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
– Squashman
Nov 9 at 18:37





I doubt your first COPY command works. The X86 directory has spaces in the folder name. So your CALL to your function will not work because of that as well. You need to put quotes around file paths with spaces.
– Squashman
Nov 9 at 18:37













sorry I forgot the quotes while trying to play with it .It works with quotes
– Rajesh
Nov 9 at 18:43




sorry I forgot the quotes while trying to play with it .It works with quotes
– Rajesh
Nov 9 at 18:43




1




1




So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
– Squashman
Nov 9 at 18:45





So if you turn ECHO ON and run the batch file from the command prompt instead of running it with your mouse, what error do you get? I know what your problem is, I am just teaching you to troubleshoot. I will give you a hint. What does your findstr command have that your copy command does not?
– Squashman
Nov 9 at 18:45













Well there is no error i donot see any error only the copy does not get created
– Rajesh
Nov 9 at 18:51




Well there is no error i donot see any error only the copy does not get created
– Rajesh
Nov 9 at 18:51












find str throws error though
– Rajesh
Nov 9 at 18:51




find str throws error though
– Rajesh
Nov 9 at 18:51












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I am going to tell you what most of us do as best practices for writing batch files.



Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.



Get into the habit of always using quotes to surround your file paths when using them with another command.



This is how I would write your batch file.



@echo off

set "_prefs=%APPDATA%testtestBrowserProfileprefs.js"
set "_prefs_notes=%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul

CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B

:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0





share|improve this answer




















  • Thanks My btch script worked now and I learnt a new thing too
    – Rajesh
    Nov 12 at 3:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231354%2fcopy-function-in-batch-script-does-not-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I am going to tell you what most of us do as best practices for writing batch files.



Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.



Get into the habit of always using quotes to surround your file paths when using them with another command.



This is how I would write your batch file.



@echo off

set "_prefs=%APPDATA%testtestBrowserProfileprefs.js"
set "_prefs_notes=%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul

CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B

:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0





share|improve this answer




















  • Thanks My btch script worked now and I learnt a new thing too
    – Rajesh
    Nov 12 at 3:53














up vote
1
down vote



accepted










I am going to tell you what most of us do as best practices for writing batch files.



Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.



Get into the habit of always using quotes to surround your file paths when using them with another command.



This is how I would write your batch file.



@echo off

set "_prefs=%APPDATA%testtestBrowserProfileprefs.js"
set "_prefs_notes=%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul

CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B

:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0





share|improve this answer




















  • Thanks My btch script worked now and I learnt a new thing too
    – Rajesh
    Nov 12 at 3:53












up vote
1
down vote



accepted







up vote
1
down vote



accepted






I am going to tell you what most of us do as best practices for writing batch files.



Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.



Get into the habit of always using quotes to surround your file paths when using them with another command.



This is how I would write your batch file.



@echo off

set "_prefs=%APPDATA%testtestBrowserProfileprefs.js"
set "_prefs_notes=%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul

CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B

:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0





share|improve this answer












I am going to tell you what most of us do as best practices for writing batch files.



Never assign quotes to variables. You can use quotes though to protect the assignment of the variable. This helps with protecting special characters within the assignment and also keeps you from assigning trailing spaces.



Get into the habit of always using quotes to surround your file paths when using them with another command.



This is how I would write your batch file.



@echo off

set "_prefs=%APPDATA%testtestBrowserProfileprefs.js"
set "_prefs_notes=%ProgramFiles(x86)%testtset1DataworkspaceBrowserProfileprefs.js"

#rem it works
copy /y "%_prefs_notes%" "%_prefs_notes%.copy1" > nul

CALL :AMEND_PREFJS "%_prefs_notes%"
EXIT /B

:AMEND_PREFJS
rem make copy of prefs file
#rem it does not work
copy /y "%~1" "%~1.copy" > nul
findstr /v "layers.acceleration.disabled" "%~1" > "%~1.tmp"
echo end
EXIT /B 0






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 19:07









Squashman

8,05431932




8,05431932











  • Thanks My btch script worked now and I learnt a new thing too
    – Rajesh
    Nov 12 at 3:53
















  • Thanks My btch script worked now and I learnt a new thing too
    – Rajesh
    Nov 12 at 3:53















Thanks My btch script worked now and I learnt a new thing too
– Rajesh
Nov 12 at 3:53




Thanks My btch script worked now and I learnt a new thing too
– Rajesh
Nov 12 at 3:53

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231354%2fcopy-function-in-batch-script-does-not-work%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus