I have these files in a folder:
aaa.txt
bbb.txt
ccc.txt
I want to prepend all the filenames with 1_, so I try to write this in a Windows command prompt:
rename * 1_*
Doing so I want to get this result:
1_aaa.txt
1_bbb.txt
1_ccc.txt
But instead i get this:
1_a.txt
1_b.txt
1_c.txt
Instead of prepending it is just overwriting the names from the start. According to this (https://www.computerhope.com/renamehl.htm) article that is indeed the intended behavior.
But in this (https://www.windowscentral.com/how-rename-multiple-files-bulk-windows-10) article they show an example where they are increasing the length of the first part of the filename like this:
ren nyc_*.* newYork_*.*
So that seems to be similar to what I want to do, but when I try that exact example it does not work like that. Again, it just overwrites the first part the name without adding anything, and then I end up with nyc_(1).jpg becoming newYork_.jpg (the unique number is overwritten).
Is the second article plain wrong? How do I simply prepend something to a bunch of files with a batch line?
One method is to knock up a temporary batch file to do your renaming.
First, dump your directory to a text file with formatting turned off:
dir *.txt /B > list.bat
Open it in Notepad and then copy that bare listing into the likes of Excel (or if you have a text editor with powerful features you can use it.) You can then create a formula, like:
="rename " & A1 & " 1_" & A1
Which will build you a list of individual rename commands changing each file one at a time, like this:
A B
aaa.txt rename aaa.txt 1_aaa.txt
bbb.txt rename bbb.txt 1_bbb.txt
ccc.txt rename ccc.txt 1_ccc.txt
Copy that new column back into notepad and save it.
Run and discard your new batchfile and everything will be renamed over.
For your nyc to NewYork, you need a bit more work...
="rename " & A1 & " NewYork" & MID(A1,4,99)
...will strip the left three characters from the name replace them to give you:
rename nyc_(1).jpg NewYork_(1).jpg
forfiles /m *.txt /C "cmd /c rename #file 1_#file"
Related
I need to rename a group of .pdf files
In the \Source folder I have the files:
bbbbbbbbb-56.pdf
vduysdvss-60.pdf
sdvbdsbvv-80.pdf
I have to rename them in the \Destination folder like this:
11111111-bbbbbbbbb-ggg-hhh-56-dddd.pdf
11111111-vduysdvss-ggg-hhh-60-dddd.pdf
11111111-sdvbdsbvv-ggg-hhh-80-dddd.pdf
so I need to insert some fixed parts:
before the name
in the middle of the name
at the end of the name.
Using the command:
cd \Source
copy "\Source" "\Destination"
cd \Destination
ren *.pdf 11111111-?????????-ggg-hhh???-dddd.*
the result is:
11111111--56-ggg-hhh-dddd.pdf
the bbbbbbbbb string disappears
can you help me?
Thanks
By using the following command Copy the files from Source to Destination :
copy "/Source_folder" "/Destination_folder"
Go in /Destination folder
cd "/Destination_folder"
And then ren the file names by the following command :
ren *.pdf ???-new_filename_part.*
(The question mark (?) is also a wildcard, but it represents a character of the original file name. So, in the syntax, we're using three question marks, which means that the output file name will include the first three characters of the original file (which works as a unique identifier to avoid duplication)
(According to your logic you can change the new filenames by using some RegExpressions or some variables)
So a quick explanation. We have hundreds of projects and in each of them and in every new project we have a program where we fill in a ton of information. From that we get 20 pdf files that are called File_1, File_2 etc.
What I'd like to do is to rename the files as the example below with a bat file.
File_1 = abc.pdf
File_2 = xyz.pdf
File_3 = qwe.pdf
I want to specific in the bat file what I want file_1 should be called and file_2 etc. The files I get will always be called File_1, File_2 etc and I always want them to be renamed the same. So each time I get those files I just run the bat file. Is there a way to do this? Or is there a better way to do this?
Thanks!
I'm assuming your files will always be called "file_1", "file_2" etc.??
If they are, then you can just write a file with a line for each file. For example
ren file_1 foo.pdf
ren file_2 bar.pdf
ren file_3 foobar.pdf
Note that as you didn't specify extensions for the original filenames, I haven't either. You would want to put the full file name with extension, or use wildcards if appropriate, e.g. ren file_1.*. (Be very careful with wildcards though, or you may end up trying to rename multiple files with the same name!)
You could check if the file exists first, and only run the ren on files that are there, or you could run it and let it error for missing files (though I'm sure other people will have reasons why you shouldn't).
You should also consider whether or not there will already be a file with the name you are trying to rename to, because if there the is then the rename will error and fail.
If your files will be different names daily, you will need to give more information as to what you have and what you need.
EDIT - Response to comment below
Copy (and complete) this code, and save it as 'rename.bat' or whatever you need.
#echo off
ren file_1 abc.pdf
ren file_2 xyz.pdf
ren file_3 qwe.pdf
<<repeat as necessary>>
The #echo off just stops the batch file from displayed the commands. It will still display errors (if the file doesn't exist or if you try to rename a file to an existing filename). You really should be looking at catching the errors and doing something with it, but if you can be 100% certain that the new filenames don't exist it will work.
Also worth pointing out that as I haven't used full file names, the batch file would need to be in the same folder as your file_1, file_2 etc.
You could use move to specify the full path of the original file, and a new path (it will move and rename the files), but you still have potentially the same problems with duplicate filenames etc.
I'm assuming you will end up with a folder containing file_1, file_2 etc. so you can just copy your batch file into the folder, run it, then move all your renamed files to where they need to go. Then next time you need to run the file, your folder would only have in it the new set of file_1, file_2 etc. so you could copy in the batch file again, run it... and so on.
EDIT2 - After thought for filenames including spaces
It just occurred to me that your existing files (file_1, file_2 etc.) don't appear to have any spaces in the name, but your new filenames might (you didn't specify the names exactly). If you have spaces if filenames, make sure to add quotes to the command e.g.
ren file_1 "my new file.pdf"
You can also quote your original filenames too (quoting both wouldn't hurt even if there are no spaces) so you could try
ren "file_1" "abc.pdf"
ren "file_2" "x y z.pdf"
ren "file_3" "qw e.pdf"
etc.
this seems like a simple one but I can't find it anywhere!
I want to zip up a file with this command:
#"a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip D:\tmp\failedImages";
This basically means that I'd like to zip up the folder "D:\tmp\failedImages" and move it to "C:\Greg_Folder\zippedPackage.zip"
THE PROBLEM: I want to zip to the "Greg_Folder" and not 'Greg Folder"... but underscores seem to be translated to spaces in the cmd line. So I need a way to say that I want the underscores to be treated literally as part of the directory name.
any ideas?
I just compressed some folder on my drive using your command without the # and the quotes.
"C:\Program Files\7-Zip\7z.exe" a -tzip -mx0 -v500m -mmt -- C:\Greg_Folder\zippedPackage.zip K:\devkitPro\devkitARM\arm-eabi
I just did not put the "#" sign which is for some other option (and your bug seems to be a strange side-effect), and it even created the "Greg_Folder" directory and the .zip.001 file inside.
I need to create a script, which concatenates multiple text files into one.
I know it's simple to use
type *.txt > merged.txt
But the requirement is to "concatenate files from same day into file day_YYYY-DD-MM.txt" I am a Linux user and Windows batch is hell for me. It's Windows XP.
Windows type command works similarly to UNIX cat.
Example 1: Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)
type file1.csv file2.csv > concat.csv
Example 2: Merge files with pattern (This will merge all files with csv extension and create concat.csv)
When using asterisk(*) to concatenate all files. Please DON'T use same extension for target file(Eg. .csv). There should be some difference in pattern else target file will also be considered in concatenation
type *.csv > concat_csv.txt
At its most basic, concatenating files from a batch file is done with 'copy'.
copy file1.txt + file2.txt + file3.txt concattedfile.txt
In Win 7, navigate to the directory where your text files are. On the command prompt use:
copy *.txt combined.txt
Where combined.txt is the name of the newly created text file.
Place all files need to copied in a separate folder, for ease place them in c drive.
Open Command Prompt - windows>type cmd>select command prompt.
You can see the default directory pointing - Ex : C:[Folder_Name]>.
Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.
After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.
Now type : 'copy *.txt [newfile_name].txt' and press enter.
Done!
All the text in individual files will be copied to [newfile_name].txt
I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.
Example 1 (files in the same folder):
copy file1.txt+file2.txt+file3.txt file123.txt
Example 2 (files in same folder):
type *.txt > combined.txt
Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):
for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
We can use normal CAT command to merge files..
D:> cat *.csv > outputs.csv
cat "input files" > "output files"
This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.
Try this:
#echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%
set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
This code ask you to set the name of the file after "day_" where you can input the date.
If you want to name your file like the actual date you can do this:
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
You can do it using type:
type"C:\<Directory containing files>\*.txt"> merged.txt
all the files in the directory will be appendeded to the file merged.txt.
copy is definitely much faster than type - but it sometimes (with large files?) adds a SUB character at the end of the file. So, strictly speaking, it does not simply concatenate the files in the same way as cat in Unix.
So, the correct answer is to use cat - either in something like Git Bash (where it has the same syntax as in Unix), or PowerShell (where it does not).
I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).