I am trying to copy a file from one location to another using a .bat file
copy ""\\PRDHILQW01\LVFPF9\MTHHEDGLIAB.TXT" "\\PRDHILFT04\L&i-ia&rs\PRODUCTION\RS-Actuarial\MTL\Download\VTG1\mthhedgliab_%yyyy%%mm%%dd%.txt" /y"
I get this error
The system cannot find the path specified.
'i-ia' is not recognized as an internal or external command,
operable program or batch file.
What do I need to do to get the &s in the file path recognized as legit part of the file path?
Why the extra quotes? The & must be either quoted, or escaped as ^&. Your extra enclosing quotes are making it so that the quoting does not work properly - you have a quoted empty string "" at the beginning, quoted spaces " " between the file names, and " \y" at the end. Everything else is unquoted.
Simply remove the outer quotes and everthing works as you want.
copy "\\PRDHILQW01\LVFPF9\MTHHEDGLIAB.TXT" "\\PRDHILFT04\L&i-ia&rs\PRODUCTION\RS-Actuarial\MTL\Download\VTG1\mthhedgliab_%yyyy%%mm%%dd%.txt" /y
Related
I have made a shortcut for opening folders based on job numbers for work. It looks through the job directory for the job number and then opens that folder. Unfortunately, this fails when the folder name has a comma "," in it:
IF %JOBNUM%==!JOBTEST! %SystemRoot%\explorer.exe %%G
Where, for example:
%%G = X:\A12300-12399\A123456 - Job with, comma
Instead of opening the job folder, it just opens My Documents (which, I assume, is the default location for Explorer.
Is there a way to let it know that the comma is part of the folder name and not some sort of delimiter?
Try adding the path inside quotes like "path"
Batch files are essentially shell commands executed as a batch. Commands are delimited by space, "," , "=" etc. As shown Here
When you use a pathname with a delimiter, windows takes the first string to be the actual value and discards the rest. Using quotes is explicitly instructing the shell to treat everything inside the quotes as a single unit. That goes the same for %%G.
I am trying to create a batch script to send a shortcut of a file to my start menu but I have ran into trouble setting the "Start in" option. I am using Shortcut from Optimum X to do it and this may be an error from their decade old program and not my code.
%~f1 = C:\Program Files (x86)\Example\Example.exe
%~dp1 = C:\Program Files (x86)\Example\
Running
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~f1" /a:c /t:%1
works as intended and creates a shortcut with the "Start in" set to the dir of the file. But running
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~dp1" /a:c /t:%1
fails when it encounters a space, for example if the path is in Program Files the error is
"The Parameter "Files" is invalid
The Syntax of the command is incorrect"
Sorry if this is not enough information I know very little Batch.
Your problem are not the spaces, but how the standard arguments are handled.
In your case, you are not passing the data to a internal command, but to a external executable that will use its own parser (the default C startup code). To this parser a backslash followed by a quote is a escaped quote that does not end the argument.
As your %~dp1 ends with a backslash, it escapes the quote, so the argument continues until the first unquoted space, in your case after Program in the /t switch. So, Files (in the /tswitch) is not recognized as a valid argument.
The usual approach is to remove the ending backslash with something like
for %%a in ("%~dp1.") do (
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%%~fa" /a:c /t:"%~1"
)
Here the for command is used to process the path to the file with an added dot. This why when we retrieve the full path to the referenced element (%%~fa) the ending backslash will not be included.
But, if the target folder is the root of the drive, this approach will not work. The ending backslash can not be removed. So, another approach is to avoid the quote scape by simply escaping the backslash
shortcut /f:"%targetfolder%\%fileName%.lnk" /w:"%~dp1\" /a:c /t:"%~f1"
Now the quote is preceded by two backslashes that will be seen as a escaped backslash followed by the closing quote of the argument.
I have got a temporary file that my script creates and it has got the following path within it
Test_Path =D:\test\
I have got this command within my script which should delete "Test_Path =" and should just leave D:\test\ in the temp file
sfk filter test1.tmp -rep "_ Test_Path =_ _"
but this is not working as "_" is a already a delimiter in this command and my string has also got an underscore in it "Test_Path"
How could i possibly do this? My updated file should just have the path D:\test.
If it's the only string in the file:
set /p string= < file.txt
echo %string:~11% > file.txt
It will write file contents (the first line) to the variable string, then remove first 11 characters (which is Test_Path=) from the variable value and write it back to the file.
I'm trying to rename a file through a system() command and I get (The filename, directory name, or volume label syntax is incorrect)
This is the line from my code:
system("ren" "C:\Users\Mac\Desktop\new folder\03 Elfsong - Shar.mp3 test.mp3");
What is the syntax problem here?
ren is a cmd.exe built-in command, it's not a standalone executable, so you'd have to do
system("CMD", "/c ren originalname newname");
The answer was escape sequence,
The syntax is:
system ("ren old_filename new_filename");
But you must use escape sequence because of the white spaces in the name of the file, and for the '\' in the path since only a white space separate between old filename and the new filename.
And also you must contain the old filename in (\"\") to specify it as (old_filename) in the syntax.
Code:
system("ren \"C:\Users\Mac\Desktop\New folder\03 Elfsong - Shar.mp3\" \"test.mp3\"");
I don't know how to describe exactly what I'm trying to do but here's an example batch file that demonstrates what I can't figure out.:
I've got a batch file. Inside that batch file I'm trying to create a directory:
Set CopyFrom = %~dp0
if Exist "%ProgramFiles(x86)" (
Set TargetDir = %ProgramFiles(x86)%\My Directory Name has spaces
)
md %TargetDir%\NewSubFolder
copy %CopyFrom%\SourceFile.zip %TargetDir%\NewSubFolder
My batch file is failing on line 4 Set TargetDir =... with:
\My was unexpected at this time
I'm assuming this is because I have spaces in my path name. I thought I could just wrap my variable with quotes:
Set TargetDir = "%ProgramFiles(x86)%\My Directory Name has spaces"
But then when I get to the line that creates the directory it fails because %TargetDir% is now wrapped in quotes. md "%TargetDir%"\NewSubFolder
Can this be fixed or should I just write a VBScript to sort things out?
Just put your expression in quotes like this:
C:\>Set "TargetDir=%ProgramFiles%\My Directory Name has spaces"
C:\>echo %TargetDir%
C:\Program Files\My Directory Name has spaces
Note: It will expand the variable within the quotes, and if it too has spaces, it will need to be quoted.
Now you can quote it to perform your operation:
md "%TargetDir%\NewSubFolder"
The problem in question here are not the spaces as others suggested, but rather the closing parenthesis in the environment variable ProgramFiles(x86) This causes the parser to think that the block ends prematurely (shameless self-promotion).
Quotes do help in this case because they make the parser jump over the whole quoted part and rightly assume the following parenthesis to be the actual closing one. but the fix might be much easier than that:
if Exist "%ProgramFiles(x86)%" Set TargetDir=%ProgramFiles(x86)%\My Directory Name has spaces
Why use a parenthesized block at all if all you do it put exactly one command into it?
set itself doesn't need any quotes, except when its arguments contain special characters like <, >, |, & which the shell itself aready handles. It isn't a panacea, though which makes handling user input or file contents correctly a pain at times.
Also, please never ever put spaces around the = in a set command. This will cause an environment variable to be created with its name ending in a space and its contents starting with a space. This was partially corrected in Windows 7 by silently creating both the variable with the space at the end and one without:
> set foo = bar
> set foo
foo=bar
foo = bar
But in previous versions of Windows this didn't happen so just never use spaces around the = unless you know this is what you want :-)