I never wrote batch files before and now I am having a requirement of renaming every file with YYYYDDMM from the filename aaaYYYYMMDD123456.csv
The code below works if there is only one file, but doesn't work if there are multiple files.
for %%F in (aaa*f.csv) do ( set "name=%%F" ren "%%F" "!name:~3,8!.csv" )
In case of multiple files the last file's YYYYMMDD is renamed for the first file and all the remaining files show the error "A duplicate file name exists, or the file cannot be found"
You can try the code below:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for %%F in (aaa*.csv) do (
set name="%%F"
ren "%%F" "!name:~4,8!.csv"
)
Related
I have an issue to rename a file to a result of a dir.
Example :
I have a folder name candidate in C drive and inside candidate folder there is below files :
something.txt
everything.doc
onething.bmp
When I dir , I will get the listing of the files.
In the folder will always be 1 txt file and 1 bmp file
How can I rename the BMP file so it will have the same name as the TXT file assuming that I use dir *.txt
In the example above how can I change onething.bmp to something.bmp
Thanks in advance for any answers
for %%X in ("*.txt") do (
for %%Y in ("*.bmp") do (
echo ren %%Y %%~nX%%~xY
)
)
remove the echo if the output is what you need.
For explanation of %%~nX etc. see for /?
This should work:
#ECHO OFF
FOR %%X in ("*.txt") DO (SET txtName=%%X)
FOR %%X in ("*.bmp") DO (SET bmpName=%%X)
FOR /F "tokens=1 delims=." %%X IN ("%txtName%") DO (
REN %bmpName% %%X.bmp
)
This will only work if the bat file is located in the same folder as the txt and the bmp files. If it's located somewhere else you will have to adjust the path.
If there are several bmp/txt files in the foleder, only the "last" files will be processed.
Since I cannot add a comment, I am asking a related question.
The original posting found here works quite well.
Is there a way to use this with a list of file names? I've seen where a file list can be passed to the ROBOCOPY command but I have been unable to get it to work.
Taking a step back, I have a series of folders and there are specific files inside of them that I want to copy out to a single folder. I have a text file which lists the names of these files.
I am looking for a batch routine that will look for each of the files in the text file in each of the folders and then copy the files out to a new folder.
Thank you!
Test this - the file.txt has a filename on each line.
It doesn't handle filename conflicts.
#echo off
cd /d "c:\base\folder"
for /f "usebackq delims=" %%a in ("file.txt") do (
for /f "delims=" %%b in ('dir "%%a" /b /s /a-d ') do copy "%%b" "d:\target\folder"
)
I recently had to tackle this problem, and many files that I wanted to move to from the hierarchy to a single folder had the same name as each other, and I wanted to still flatten the hierarchy without them to being over-written.
What I did was write a script that moves the file, but renames it with the old hierarchy path in the name
for example:
source files:
C:\files\somefiles\file.txt
C:\files\otherfiles\file.txt
destination is C:\newdir\
files are created as
C:\newdir\somefiles-file.txt
C:\newdir\otherfiles-file.txt
here is the code, batch file 1 goes thru the files, batch file 2 renames and moves them (could also copy instead, if you want to preserve the source:
#echo off
for /r %%f in (*.*pr) do #renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"
renameandmovefilespart2.bat
#echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem #echo shortened: %newname%
rem #echo source path: %origPathOnly% newPath: %startingDir%
rem #echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"
I have a batch file that lists the full file directories (including filename at the end) onto a csv file. I need it to produce this, but also just the filename in a separate column. I would like it so that the format is Filename in first column (including extension) and full file directory in second column. The batch file I currently have is:
dir C:\Users\Administrator\Desktop\test\Files\*.tif /b /s >>
C:\Users\Administrator\Desktop\test\Output.csv
EDIT: I forgot to mention the 'Files' folder contains many subfolders so I need it to process all files from all these subfolders.
Thanks in advance.
You just need to do this:
#echo off
setlocal
set "in=C:\Users\Administrator\Desktop\test\Files\*.tif"
set "out=C:\Users\Administrator\Desktop\test\Output.csv"
if not exist "%out%" type nul>"%out%"
for /f "delims=" %%a in ('dir /b/s %in%') do (
>>%out% echo "%%~nxa","%%a"
)
Iterate and write (help for for info on the %~ variable modifiers):
#echo off
cd C:\Users\Administrator\Desktop\test\Files
for %%f in (*.tif) do echo "%%~nxf","%%~dpf" >> Output.csv
I want to copy several unique PDF files to a unique folder.
The folders already exists.
For example
C:\Document\240C03881_10.pdf Copy this one to : C:\Endresult\240C03881\240C03881_10.pdf
C:\Document\240C03882_10.pdf Copy this one to : C:\Endresult\240C03882\240C03882_10.pdf
C:\Document\240C03883_10.pdf Copy this one to : C:\Endresult\240C03883\240C03883_10.pdf
The script should only read the first 9 digits. The script may not read _10.
Example,
Script sees 240C03881_10. But read it as 240C03881. The script is going to look if the 240C03881 folder exists. If not, the script ends/ignores it. If it does exists, it places the .pdf to its corresponding location.
This is the script i currently have, but nothing happens.. anyone? :
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-3!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
for %%p in (*.pdf) do for /f "tokens=1 delims=_" %%n in ("%%~np") do (
copy "%%~fp" "c:\endresult\%%~n\%%~nxp"
)
I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1