Batch - Rename Filetype and overwrite file - batch-file

I have a batch file which is using the following code:
cd ..\changed
for /f "delims=" %%i in ('dir /b *.txt') do (
ren "%%i" "*.xxx"
)
The change of filetype works great, but there is a problem when in folder changed are two files with the same name. Is there any posibility how to overwrite file with the same name using similar code?
Thank You

Related

Batch rename file invert position name

My files have names like:
andromeda_01.02.19.xlsx
I need to change it to:
andromeda_20190201.xlsx
Removing the dots, using a batch file.
I could only isolate the date, using batch:
#echo off
RENAME "andromeda_*xlsx" "//////////////////*.xlsx"
Can someone help?
For andromeda_dd.mm.yy.xlsx files, you should use:
#echo off
for /F "tokens=2-4 delims=._" %%A IN ('dir /B /A-D andromeda_*.xlsx') do (
rename "andromeda_%%A.%%B.%%C.xlsx" "andromeda_20%%C%%B%%A.xlsx"
)
This will ensure that we do not rename files already renamed. i.e only rename andromeda_dd.mm.yy.xls and wil not rename andromeda_yyyymmddxls Additionally, we will check if a file ro rename to does not exist already and tell you about it if it does. Also, this assumes the century 20yy as there are no other indicators in your filename proving another:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1* delims=_" %%i in ('dir /b /a-d andromeda_*.*.*.xlsx') do (
set "manip=%%~nj"
if exist "%%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj" (
echo file %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj already Exists!
) else (
rename %%i_%%j %%i_20!manip:~6,2!!manip:~0,2!!manip:~3,2!%%~xj
)
)

Batch file to copy the file from a specific folder and not the sub folders to the target folder

I tried to execute the below script, which copies the latest file from one folder to another folder. But I tested the below script in different scenario, in which it even copies the file CC*.txt if it exists in sub folders apart from C:\Source.
But I wanted to copy the latest file from only the source directory (C:\Source) mentioned in the script below, but if the same new file exists in any of the folders/sub-folders should NOT be taken to consideration, kindly suggest.
FOR /F %%I IN ('DIR "C:\Source\cc*.txt" /B /A:-D /T:W /O:D /S') DO SET "LATEST=%%~I"
COPY "%LATEST%" "C:\Destination"
PS: Mostly because of /S, but I am not sure what option need to use to pick the file only from the specified directory C:\Source, kindly suggest.
Simply remove the /S option (which tells dir to process sub-directories also):
FOR /F %%I IN ('DIR "C:\Source\cc*.txt" /B /A:-D /T:W /O:D') DO SET "LATEST=%%~I"
COPY "C:\Source\%LATEST%" "C:\Destination"
Since dir /B without the /S switch returns the pure file names only, you need to include the source location in the copy source once again for the script to work from any working directory.
Try this :
for /f "tokens=*" %%A in ('dir /b /s /O:D "C:\Source\cc*.txt"') do set "latest=%%A"
copy "%latest%" "C:\Destination"
FOR /F %%I IN ('DIR cc*.txt /B /O:-D') DO COPY %%I "C:\Destination" & EXIT
Super easy.
FOR /F "delims=|" %%I IN ('DIR "cc*.txt" /B /O:D') DO COPY %%I "C:\Destination"
Both should do

Create folders based on part of a filename

I have an old archive system that has died and want to create a batch file to put pdf files into a folder based on part of the file name. There are 1000's of files in one directory, example file name abc12345620110101.pdf.
I need the batch file to create a folder based on the 4th to 9th character "123456" (accountnumber) from the example above and then place the file in that folder.
X:\\123456\\abc12345620110101.pdf
I will have multiple files that will go into the same folders so the batch needs to work even when the folder already exists.
Thanks in advance.
P.S. I have tried the following:
setlocal enabledelayedexpansion
for /f %%i in ('dir /b *.PDF') do (
set filename1=%%i
set folder1=!filename1:~4,20!
mkdir !folder1!
but I had no luck and I am open to any batch that will work.
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~3,9!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)

Rename A Group Of Files

I need to rename a group of files in the same folder.
When I try to run the batch file, it doesn't work correctly:
`ren *.txt Updated_*.txt`
The file names contain date_names_location.txt, examples are below
08232013_name1_nyc.txt
08212013_name1_nyc.txt
08232013_name1_la.txt
08212013_name1_la.txt
When I run the batch file I get back:
Updated_1_name1_nyc.txt
instead of
'Updated_08232013_name1_nyc.txt'
Any ideas on how to fix? Thanks
This is one way:
#echo off
for /f "delims=" %%a in ('dir /b /a-d *.txt') do ren "%%a" "Updated_%%a"
REN has no insert mode, so it just replaces the beginning of your file names. Try the solution provided here
Batch renaming files using Windows 7 REN (adding prefix)?

batch file picking up files from directory and subdirectories

I have a batch file that is appending a date to all to the file name of all CSV files. I only want CSV files in one directory to be picked up and no subdirectories. It appears to be running through all subdirectories though.
I have this code currently in the batch file
:: copy files
For /f "delims=" %%a in ('Dir /A:-D /b /s "%LOCALDIR%"*.csv 2^>nul') do If exist "%%a" (
COPY "%%a" "%LOCALDIR%%dtt%-%%~na.csv"
DEL "%%a"
)
I have tried getting rid of the /s in the code but then no files are picked up in the directory I want to look for.
Any help is greatly appreciated.
Why not just use a simple loop like?
pushd %LOCALDIR%
for %%A in (*.csv) do ren "%%~A" "%dtt%-%%~A"
popd
Or for a one liner
for %%A in (%LOCALDIR%\*.csv) do ren "%%~A" "%dtt%-%%~A"
If the path has spaces in it remember to use the usebackq option.

Resources