Remove section of filename - filenames

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.

Related

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

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"

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

Almost working PDF merge batch file, just a little help needed

I'm using this batch file to merge PDF files, and it's working, but only with files which don't have spaces in their names. For a 'test file.pdf' it gives an error ~ can't find file test and file.pdf. With what should I replace the "*.PDF" to get it to work correctly?
#echo on
setlocal enabledelayedexpansion
FOR %%A IN ("*.PDF") DO (set command=!command! %%A)
%~dp0\pdftk.exe %command% cat output "%~dp1merged_PDF.pdf"
Replace set command=!command! %%A with set "command=!command! "%%~A"".
For naming the merged file after its parent folder you can determine the parent folder name like this:
for %%a in ("%~dp1.") do set "name=%%~nxa"
and then create the output file like this:
%~dp0\pdftk.exe %command% cat output "%~dp1%name%.pdf"

How do I use a for loop to get file names and then use them?

I have a folder with the files a.txt, b.txt, and c.txt in it. and I want to use a for-loop to get the names of these files, store them in a variable, and then add them to another text file. The text files are located in the same file as my bat file. Is it possible to do this? If so please show me, these for loops confuse the crap out of me...
What i have now
#echo on
SET name=hey
echo >text.txt
for %F in (*.*) do (set name=#fname
echo name >> text.txt)
#Echo OFF
(For %%# in ("*.txt") do (
Set "FileName=%%~n#"
Call Echo %%FILENAME%%
))>"MyFilenames.txt"
Pause&Exit
NOTE 1: Files are stored in "Filename" var, but is not really necessary, you can directly write the filename to the textfile.
NOTE 2:If you want a recursive loop through files use the /R switch of For command.
NOTE 3:If you want also the file extension change this: "%%~n#" to this else: "%%~nx#"
**UPDATE:**
An alternative script:
#Echo OFF
For %%# in (*.txt) do (Echo %%~n#>> "MyFiles.txt")
Pause&Exit

XP Batch scripting - zipping with rinrar looping through a directory *.csv

I have read numerous articles now and it's not clear and there's lots of versions and this that and the other and I have been piecing things together and have got so far, my problem is the 'rar' command doesn't seem to accept my substition variable and instead reads it as a string.
But this is what I have
#echo off
SETLOCAL
set path=%path%;"C:\TEMP\Output"
set _sourcedir=C:\TEMP\Output
set _logfile=c:\temp\Output\zip_log.txt
set _rarpath=C:\Program Files (x86)\WinRAR
echo Starting rar batch > %_logfile%
:: Set default directory
pushd %_sourcedir%
echo Scan Directory is %_sourcedir%
FOR %%f IN (*.txt) DO (
echo %%f
%_rarpath\rar.exe a test
)
popd
ENDLOCAL
#echo on
I have cut some out and chopped it so you only get the essence, I haven't omitted any commands though.
I am trying to loop through the directory and locate all .txt files and zip them into a .rar file.
The echo writes out the correct filenames.
Any ideas?
I think this is your problem:
set _rarpath=C:\Program Files (x86)\WinRAR
In batch files, the environment variable delimiter is a space, so it thinks _rarpath is C:\Program
Enclose the path in double quotes and see if that helps:
set _rarpath="C:\Program Files (x86)\WinRAR"
Also, in your FOR loop change
%_rarpath\rar.exe a test
to
%_rarpath%\rar.exe a test
(or,perhaps this was a typo?)
I don't see where you're asking winrar to do anything with your files? %%f needs to be on the winrar command line somewhere.
Also, you shouldn't need a loop at all for this: rar.exe a test.rar %yourpath%*.csv or similar.