Rename Multiple Files in a folder using batch script - batch-file

I have a folder called TEST. Inside there are 30 files.
Example:
DIM1_UPI_20170102.TXT
DIM2_UPI_20170908.TXT
DIM3_UPI_20180101.TXT
...
I have to rename them by removing the date tag
Exapmple:
DIM1_UPI.TXT
DIM2_UPI.TXT
DIM3_UPI.TXT
Can you please help me writing this in batch file?

Assuming your files are all starting with DIM
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.TXT" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-9!%%~xi
)
Once you can confirm that it does what you want, and ONLY then, remove the echofrom the last line to actually rename the files.
Important Note. If you have files with similar names, but different date entries, this will not work as you think. as Example:
DIM2_UPI_20170910.TXT
DIM2_UPI_20170908.TXT
The names are the same, but dates differ, making each filename Unique. If you rename them, there can be only 1 DIM2_UPI.TXT So as long as you understand this, you will be fine.
Edit: based on Amazon drive question. Note you need to change the directory portion to how you access amazon drive.
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "DIM*" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-16!%%~xi
)

Related

Using a batch file, how do I delete large, old files?

I have temp files which are taking up a lot of disk space which I want to tidy on a scheduled basis using a batch file.
The file names I want to deal with always start with "harp_" but I want to keep the most recent 2 files.
For example the folder contains files such as:
harp_0tnqy2o078824m1ax 25/10/2016
harp_0e3qmw11gz6z9j10s 24/10/2016
harp_0tnqy2o078824m1ax 23/10/2016
harp_11ik03u00g4k2y19y 22/10/2016
Test 1 25/10/2016
Test 2 10/10/2016
I want the folder to just now contain:
harp_0tnqy2o078824m1ax 25/10/2016
harp_0e3qmw11gz6z9j10s 24/10/2016
Test 1 25/10/2016
Test 2 10/10/2016
Is anyone able to help me script the batch file for this?
You can use the FOR /F command to parse the output of the DIR command. The key is using the SKIP option so that it keeps the two newest files.
FOR /F "skip=2 delims=" %%G IN ('DIR /A-D /B /O-D harp_*') DO del "%%~G"
I'm quite unshure if batch ot bash is required. In case of batch my answer is like Squashman's adding an aspect of swecurity, files to be deleted are added to a tmp-file which might be revised edited before execution.
#Echo off&Setlocal
Set Basefldr=C:\where\ever
PushD "%Basefldr%" ||(Echo Couldn't cd to %Basefldr% &Pause&Exit /B 1)
Set DelList="%tmp%\DelList.cmd"
Type NUL>%DelList%
For /f "skip=2 delims=" %%A in ('Dir /B/A-D/O-D "harp_*"'
) Do >>%DelList% Echo:Del %%~fA
:: Review the file DelList.cmd before executing it
Notepad.exe %DelList%
:: or
:: more %DelList%
popd

Batch copy multiple files from different folders with their paths listed in a txt file, and rename any duplicates

I've been trying to tackle this problem for few days now to no avail. I have no programming experience whatsoever and this task has been driving me nuts.
I have a txt file that has a list of paths to files that need to be copied. Some 8000 paths are in this file.
Copying each item isn't such a big deal as I can just add the copy command and destination before/after each path.
The crux of my issue is that many of these files have the same filename and when they're in different directories it's not a problem.
However I need all the files in the same destination folder and it keeps overwriting itself.
To sum up, I have a .txt file that basically looks like this:
D:\Big folder\Folder\Subfolder a\filea.file
D:\Big folder\Folder3\Subfolder za\filek.file
D:\Big folder\Folder\Subfolder ds\filed.file
D:\Big folder8\Folder\Subfolder p\filea.file...
I need some tool that will let me copy all of these files into one destination folder, and make sure any duplicates get renamed so that they aren't overwritten.
such that filea.file and filea.file become filea.file and filea1.file
EDIT: so far I've come up with
FOR /F "tokens=* usebackq" %i IN (`type "C:\Users\username\Desktop\completelist.txt"`) DO COPY "%i" "E:\destination\"
which does the read and copy job but not the rename part
Save script below to Copy.bat, open Cmd Prompt from the script directory, and run the bat. It works well for me. Post exact errors, if any.
#echo off
setlocal enabledelayedexpansion
set file=%userprofile%\Desktop\completelist.txt
set "dest=E:\destination" & set "i=" & pushd !dest!
for /f "usebackq tokens=*" %%G in ("%file%") do (
call :rename %%~nG %%~xG %%G
copy "%%G" "%dest%\!target!" >nul )
popd
exit /b
:rename
set "target=%1!i!%2"
:loop
set /a i+=1
if exist "!target!" set "target=%1!i!%2" & goto :loop
set "i=" & echo Copied %3 to !target!
exit /b

finding textfile name by pattern and moving to newly created folder

I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal

Using batch files to remove a single number from a folder name

At work we use a bespoke program to search through a directory tree and find an individual image file. These files are stored in folders that have a 7-digit name, starting with '18' - so for instance '1873456', '1873457', '1873458' etc. The problem I have is at some point last year the procedure that creates these folders and populates the images in them reached '1899999' - and then rolled over to '18100000' and carried on like that for over 4,000 folders before it was caught and corrected.
The bespoke program we use can only handle seven-digit folder names. What I would like to do is create a batch file that renames all the eight-digit folders by removing the extra '1' in the name, so '18100000' becomes '1800000' and so forth until '18104013' becomes '1804013'.
Can anyone help?
Run this in the base folder, it will not change any folders.
A file called renfile.bat.txt will be created that contains the rename command for the folders that match the filespec. Examine it in notepad to see if it is ok and then rename it to renfile.bat and run it.
It's not tested.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (18??????) do (
set "name=%%~nxa"
>>renfile.bat.txt echo ren "%%a" "!name:~0,2!!name:~3!"
)
Something like
for /l %%x in (100000,1,104013) do (
set oldsuffix=%%x
set newsuffix=%oldsuffix:~-5%
ren 18%%x 18%newsuffix%
)
setlocal enableextensions enabledelayedexpansion
for /d /r "c:\somewhere" %%f in (181?????) do (
set "name=0" & set /a "name+=%%~nf" 2>nul
if !name! gtr 1899999 ren "%%~ff" "18!name:~-5!"
)

Batch Script assistance needed

Happy Friday Think-Tank!
I need some assistance with a Batch .BAT script. Specifically I need help with some "IF statement syntax"
I have a script that is renaming files. There are two files, one ending in four digits and the other ending in five digits. The files will be renamed with variables I have already pre-set earlier within my script.
So here is a scenario: We have two files in a directory located at
c:\Users\username\Desktop\test-dir
There are two files within test-dir:
file1.12345
file2.1234
A four digit ending is one variable type (VAR1), whereas a file ending in five digits is another variable type (VAR2).
I need an if statement to:
a) read all the files(s) with the chosen directory (without using a wildcard if possible).
b) determine based on the number of digits after the "." which variable to use.
c) once making that determination rename the file with the appropriate variables.
The final re-naming convention is as so: yyyymmddtype.1234/12345
So basically it would use the datestamp variable I already created, the type variable I already created to be injected by the if statement, and append with the original ending digits of the file.
I know this seems like a lot, but I am more so a bash script guy. I have all the elements in place, I just need the if statement and what feels like a for loop of some kind to tie it all together.
Any help would be great!
Thank you!
Sorry, not the option you where asking for. Instead of iterating over the full list checking each file for extension conformance, iterate over a list of patterns that will filter file list, renaming matching files with the asociated "type"
for %%v will iterate over variable list, for %%a will split the content of the variable in pattern and type, for %%f will generate the file list, filter with findstr using the retrieved pattern and rename matching files with the corresponding "type"
Rename command is preceded with a echo to output commands to console. If the output is correct, remove the echo to rename the files.
#echo off
rem Variables defined elsewhere
set "folder=c:\somewhere"
set "timestamp=yyyymmdd"
rem Rename pattern variables in the form pattern;type
set "var1=\.....$;type1"
set "var2=\......$;type2"
set "var1=\.[^.][^.][^.][^.]$;type1"
set "var2=\.[^.][^.][^.][^.][^.]$;type2"
setlocal enableextensions disabledelayedexpansion
for %%v in ("%var1%" "%var2%") do for /f "tokens=1,* delims=;" %%a in ("%%~v") do (
for /f "tokens=*" %%f in ('dir /a-d /b "%folder%" ^| findstr /r /c:"%%~a"') do (
echo ren "%folder%\%%~f" "%timestamp%%%~b%%~xf"
)
)
endlocal
#ECHO OFF &SETLOCAL
set "yyyymmdd=yyyymmdd"
set "VAR1=VAR1"
set "VAR2=VAR2"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\....."') do echo(ren "%%~a" "%yyyymmdd%%VAR1%%%~xa"
for /f "delims=" %%a in ('dir /b /a-d^|findstr /re ".*\......"') do echo(ren "%%~a" "%yyyymmdd%%VAR2%%%~xa"
remove echo( to get it working.
If I understand you then this will rename the two files using preset variables for each one:
for %%a in ("%userprofile%\Desktop\test-dir\*") do (
if "%%~xa"==".12345" ren "%%a" "%variableA%-%variableB%%%~xa"
) else (
ren "%%a" "%variableC%-%variableD%%%~xa"
)
)

Resources