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=
batch-file
|
show 5 more comments
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=
batch-file
I doubt your firstCOPYcommand works. TheX86directory has spaces in the folder name. So yourCALLto 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 turnECHO ONand 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 yourfindstrcommand have that yourcopycommand 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
|
show 5 more comments
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=
batch-file
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
batch-file
edited Nov 9 at 18:42
asked Nov 9 at 18:21
Rajesh
88141538
88141538
I doubt your firstCOPYcommand works. TheX86directory has spaces in the folder name. So yourCALLto 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 turnECHO ONand 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 yourfindstrcommand have that yourcopycommand 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
|
show 5 more comments
I doubt your firstCOPYcommand works. TheX86directory has spaces in the folder name. So yourCALLto 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 turnECHO ONand 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 yourfindstrcommand have that yourcopycommand 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
|
show 5 more comments
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
Thanks My btch script worked now and I learnt a new thing too
– Rajesh
Nov 12 at 3:53
add a comment |
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
Thanks My btch script worked now and I learnt a new thing too
– Rajesh
Nov 12 at 3:53
add a comment |
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
Thanks My btch script worked now and I learnt a new thing too
– Rajesh
Nov 12 at 3:53
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I doubt your first
COPYcommand works. TheX86directory has spaces in the folder name. So yourCALLto 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 ONand 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 yourfindstrcommand have that yourcopycommand 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