Syntax error with system() command to rename a file in C - c

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\"");

Related

Batch %~f1 works but %~dp1 fails because of spaces

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.

Escape an asterisk in DOS

I would like to rename a file in DOS. As a result, a dot and an asterisk should be appended to the file name. For example:
rename text.log text.log.*
However this returns:
text.log..
I have also tried to escape the * as follows:
rename text.log text.log.\*
But no way! Do you guys know how I can rename this file appending an .* to the string text.log by using a cmd shell?
Thanks!
You can't - * is a reserved character and so cannot be used in a filename.
https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words

how to specify file path that includes & in .bat file

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

Batch: % in file name

I think this is a simple batch programming question, but after much searching, I can't find the answer.
I'm trying to figure out how to refer to files whose names contain difficult characters.
It seems that double quotes have the effect of treating most enclosed characters literally. For example, for a file named ^^.txt, dir "^^.txt" will find the file, while dir ^^.txt won't.
However, I don't know how to escape %. For example, for a file named %ERRORLEVEL%.txt, none of these find the file:
dir "%ERRORLEVEL%.txt"
dir "%%ERRORLEVEL%%.txt"
dir "^%ERRORLEVEL^%.txt"
Any suggestions would be appreciated.
The escape character for the Windows command prompt is ^. It works without the quotes.
dir ^%errorlevel^%.txt
For your other example ^^.txt, use:
dir ^^^^.txt
Always avoid % and ^ in filenames. Rename % to 'percent' in a global file manager.
They are next to impossible to handle properly in many batch commands.
In a batch script you must double the %, eg.:
#echo off
dir %%errorlevel%%.txt
Btw. you should not use cmd default variable or command names for other things (errorlevel).

How do I set an environment variable to a value with spaces in a batch file?

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 :-)

Resources