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
Related
I have tried creating a batch file that moves mods from the gta v directory to another when i need to play online. I wrote this so far:
move E:/Epic Games/GTAV/TrainerV.asi C:/Users/example/Desktop
I got this error:
The syntax of the command is incorrect.
Can someone tell me how to do it right? I would appreciate it.
To be able to operate with any file with a space in cmd/batch, you need to group them with quotes "" and since
E:\Epic Games\GTAV\TrainerV.asi
^ Here
has spaces, you need to surround it with quotes. A good practice is to have quotes anyways. The correct syntax is:
move "E:\Epic Games\GTAV\TrainerV.asi" "C:\Users\example\Desktop"
You have multiple issues with your current command.
First, paths containing spaces must be surrounded in double quotes.
Second, Windows uses the backslash \ instead of the forward slash / as the path separator. While the forward slash might be accepted in some cases, it won't work in all, so it's better to use the proper \.
The corrected version of your code would be
move "E:\Epic Games\GTAV\TrainerV.asi" C:\Users\example\Desktop
I am new to batch files but was looking to create one that would use the tree command to write a .txt file of the directory in which the batch file is located and then convert that .txt file to a Word file or PDF that the every day user could view.
This was my attempt at the first part of the process;
tree %~dp0 > %~dp0/"Folder Contents.txt" /A /F
but this gave me an "Invalid Path" message in the .txt file
If anyone could point me in the right direction it would be greatly appreciated.
Thanks in advance for your help.
JosefZ has placed the correct command in the comments but I thought I would add a bit of an explanation here:
Think of how you path would look like with your command. It would be like C:\PathTo\Batch\/"FolderContent.txt" which actually is not a valid path.
%~dp0 will end with a \ so the / in the command from the question is syntactically incorrect.
Further the double quotes should be placed around the total path as JosefZ did in his comment. If you do not do that the path will contain these quotes and will be invalid.
JosefZ further added double quotes and a single . around and at the end of %~dp0 after your tree command. The quotes are there to include possible whitespaces into your folder-path and the . to include folders with subfolders as well. You will get an error message if you do not.
Last thing is that he placed the output file at the beginning of the command and got rid of spaces. You can basically place your output file anywhere however. Although you should notice that you should not contain spaces between > and your output-file-path.
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 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).
I receive files which names contain spaces and change every week (the name contains the week number)
IE, the file for this week looks like This is the file - w37.csv
I have to write a script to take this file into account.
I didn't succeed in writing this script.
If I do :
$FILE="This is the file - w*.csv"
I don't find /dir/${FILE}
I tried "This\ is\ the\ file - w*.csv"
I tried /dir/"${FILE}" and "/dir/${FILE}"
But I still can't find my file
It looks like the space in the name needs the variable to be double-quoted but, then, the wildcard is not analysed.
Do you have an idea (or THE answer)?
Regards,
Olivier
echo /dir/"This is the file - w"*.csv
or — you almost tried that —
echo /dir/This\ is\ the\ file\ -\ w*.csv
Use a bash array
v=( /dir/This\ is\ the\ file - w*.csv )
If there is guaranteed to be only one matching file, you can just expand $v. Otherwise, you can get the full list of matching files by expanding as
"${v[#]}"
or individual matches using
"${v[0]", "${v[1]}", etc
First of all, you should not use the dollar sign in an assignment.
Moreover, wildcard expansion is not called in an assignment. You can use process substitution for example, though:
FILE=$(echo 'This is the file - w'*.csv)
Note that the wildcard itself is not included in the quotes. Quotes prevent wildcard expansion.