Using a Batch file can you remove part of a file name from files in a folder and all sub folders? - batch-rename

Hi I am new at using batch files and I am struggling to find a way of removing part of a file name for multiple files in a folder and all sub-folder.
the files are all named like r1_c02_200111_145423_am.csv and I need to remove the _am from the files.
I have tried the following
FOR /R "C:\Users\bob\Documents\data\" %%G IN (*_am.csv) DO REN "%%G" *.csv
but this does not change anything.
can anybody point me in the right direction please?

With the help of such a script, you can perform the required operation.
#ECHO off
FOR %%i IN (*_am.csv) DO CALL :do_rename %%i
GOTO :eof
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"
:eof
Now more.
The search in the question was correct. To perform a rename while passing through the FOR ... DO, you can use both the SETLOCAL EnableDelayedExpansion or a procedure call. I think it's easier to use a procedure call. you don’t have to puzzle over understanding (and misunderstanding) how the SETLOCAL EnableDelayedExpansion works.
For each iteration of the loop, the Wick procedure is called with the first argument of the file name passed to it:
CALL :do_rename %%i
In the procedure itself, the argument is converted to a variable. You can already use substring operations on the variable itself. More information about substring operations can be found here.
:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"

Related

Remove section of filename

I am needing a batch file to remove a certain part from multiple filenames in the same directory.
Example:
I have over 80,000+ files with the title like so:
Test Title, The - Conspiracy.zip
I am needing ", The" removed from file names leavin the titles like so:
Test Title - Conspiracy.zip
PS, I am needing this in Batch file only!
Any help is much appreciated!
THANX!!!
I found what I needed to use and thank you all for the quick replies and help!
#echo off &setlocal
set currentDirectory="%CD%"
for /f "delims=" %%a in ('dir /b /a-d *, The*.*') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:, The=!"
ren "!fname!" "!nname!"
endlocal
)
If you can use a Unix command shell, you could use the mv command in a loop.
You could download cygwin or Git Bash, or if you have Windows 10, you could do this right in the command line (assuming you've updated):
Creating a file like this
#!/bin/bash
for file in *.zip
do
removedPart=", The"
mv "${file}" "${file/removedPart/}"
done
You might want to test the command on a single file first to be sure it does what you want. i.e.
file=Test Title, The - Conspiracy.zip
removedPart=", The"
mv "${file}" "${file/removedPart/}"
You can loop through the contents of the file directory in something like this loop. Batch script loop
Then when you're looping through you can replace the contents of the file name. Look at this: String replacement in batch file
Sorry not more specific as Batch scripting isn't my thing. But this logic should prove to at least be helpful. Someone my post something better.

Removing everything after specific character in batch

SETLOCAL ENABLEDELAYEDEXPANSION
set "s=DIR D:\MyFolder /S /Q ^|FIND /i "Owner" ^|findstr /m /i "\.mkv$""
for /f "Tokens=5,6*" %%a in ('%s%') do (
SET _endbit=%%aa:*STRING=%
CALL SET _result=%%aa:%_endbit%=%%
>>%tmp%\list.txt echo %_result% %%b %%c
)
wscript "C:\my.vbs"
I am listing my files that owned by Owner and has extension mkv from MyFolder. I want to remove everything after specific character/word. I wrote that code. But It seems to be not working.
First of all, is it possible to do that? If so what is wrong with my code?
You came up with the same solution I was going to post on your other question - but I think you're missing some things. The line ending STRING=% is missing another % at the end, and the line %%aa:%_endbit%=%%, I'm not sure it can work directly on the variable from the for loop, and you need to store it in another variable first, and you probably need some expansions using ! characters too.
Here's what I had that seems to work, just as a test in a folder with three files in it, removing the end of the filename using E0 as the cutoff string:
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
for %%f in (*.mkv) do (
set FULLNAME=%%f
set ENDTEXT=!FULLNAME:*E0=!
call set TRIMMEDNAME=%%FULLNAME:!ENDTEXT!=%%
echo !TRIMMEDNAME!
)

Set file name with * as variable batch script

I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd

How can i remove the extension of a filename which is saved in a batch variable?

first my batch script snippet (not working):
set _WRx32=wrar420.exe
set _WRx64=winrar-x64-420.exe
set _WRLNG=rarlng.rar
FOR %%f IN (_WRLNG, _WRx64, _WRx32) do set "%%f"="%%~nf"
[ for every variable name (!) in the list remove the extension from the variable content (!)
and save back to the same variable ]
Question: How can i remove the extension of the filenames which are saved in the
variables _WRLNG, _WRx64, _WRx32 and save this filename (without extension)
back to the same variables ??
i need something like: set var=%var~n% but that doesn't work. And in a FOR loop i don't know
how to convert the name from the (...variable names ...) list to the corresponding variable
so i can apply a parameter extension like "~n", "~nf" etc. on it.
Can anyone help me ?
Thanks
#echo off
setlocal EnableDelayedExpansion
set _WRx32=wrar420.exe
set _WRx64=winrar-x64-420.exe
set _WRLNG=rarlng.rar
FOR %%f IN (_WRLNG, _WRx64, _WRx32) do (
FOR %%g in (!%%f!) do set "%%f=%%~ng"
)
Try this:
FOR /f "delims=." %%f IN ("%_WRx32%") do set "_WRx32=%%f"
FOR /f "delims=." %%f IN ("%_WRx64%") do set "_WRx64=%%f"
FOR /f "delims=." %%f IN ("%_WRLNG%") do set "_WRLNG=%%f"
I tried a similar version on the command line, and it appears you can't do multiple variable variables in a for loop (that I know of).
So, you have two options:
If you know that the variables will always have a 4 character extension (including the period), then you could use something like set _WRLNG=%_WRLNG:~0,-4%. This will take the last 4 characters from the variable.
You have to do one loop per variable, and you need to fix up the way you are doing them. The proper way would be for /f %%i in ("%_WRLNG%") do set _WRLNG=%~ni.
Or, look at Endoro's answer for a better for loop example.
EDIT: Thought of another way:
#echo off
for /f "delims=. tokens=1,2" %%i in ("%var%") do set var=%%i

batch files, variable expansion and substrings in loops

I can't wrap my head around the variable substitution mechanism of DOS batch files, especially in for loops.
Simplified batch problem: imagine the following directory
01foo.txt
02foo.dir (this is a directory)
bar01 (this is a directory)
bar02 (this is a directory)
i want to move all files/directories in this directory that do NOT start with 'bar' to a subdirectory that is bar+_the_first_2_characters_of_the_filename_or_directory_name.
In this case, 01foo.txt would be moved into bar01 and 02foo.dir would be moved into bar02.
The following script is my first attempt:
setlocal EnableDelayedExpansion
for %%A in (*) do (
set _x=%%A
if (!_x:~0,2! NEQ "bar") move %%A bar!_x:~0,2!
)
endlocal
apart from the fact that this seems to loop only for files, it simply doesn't work at all :-).
I get an error in the if statement saying "3! was unexpected at this time"
Any idea on how to improve the situation/script?
Thanks
It's only a problem of the syntax...
The IF statement does not expect not accept surrounding brackets.
The the comma in !_x:~0,2! breaks the IF-statment, you could quote both parts or move it into an own set prefix=!_x:~0,2!" line.
If you quote "bar" you also need to quote "!prefix!".
That's all
setlocal EnableDelayedExpansion
for %%A in (*) do (
set "_x=%%A"
set "prefix=!_x:~0,2!"
if "!prefix!" NEQ "bar" move %%A bar!prefix!
)
endlocal