Windows Batch File, how to rename and remove part of filename - windows

I am trying to rename a group of files in a directory that all have a part in the filename I want removed later on.
The example of names are:
123876.111thepartIwanttoremove.exe
thisisatestfile.111thepartIwanttoremove.exe
this?392!.111thepartIwanttoremove.exe
thankyouall.222thepartIwanttoremove.exe
test.222thepartIwanttoremove.exe
whatis#d354.222thepartIwanttoremove.exe
My code is:
forfiles /S /M *.111thepartIwanttoremove.exe /C "cmd /c rename #file #fname.doc"
ren ???.111thepartIwanttoremove.* ???.doc
ren ????.111thepartIwanttoremove.* ????.doc
ren ?????.111thepartIwanttoremove.* ?????.doc (and so on)
forfiles /S /M *.222thepartIwanttoremove.exe /C "cmd /c rename #file #fname.jpg"
ren ???.222thepartIwanttoremove.* ???.jpg
ren ????.222thepartIwanttoremove.* ????.jpg
ren ?????.222thepartIwanttoremove.* ?????.jpg (and so on)
So for an example I want the file:
123876.111thepartIwanttoremove.exe
To look like this:
123876.doc
what function can I use to remove the .*thepartIwanttoremove.exe part of the name afterwards without writing so many lines with "?"?
Thank you very much for your help.

Assuming all the files have three parts to the file name that are separated by periods, this style of code should work for you.
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.111*.exe') do rename "%%G.%%H.%%I" "%%G.doc"
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.222*.exe') do rename "%%G.%%H.%%I" "%%G.jpg"

Related

How can I Delete Files That Do Not Contain Certain Characters Using Windows Batch File?

How can I delete files that do not end with the pattern .PDF_*.pdf ?
AA00A6E2.PDF
AA00A6E3.PDF
AA00A6E3.PDF_01.pdf
AA00A6E3.PDF_02.pdf
AA00A6E3.PDF_03.pdf
I have been trying all sorts of variations around the following syntax:
FOR %%F IN (%varFolderSource%\*.*) DO IF NOT "%%~xF" == "*_*" DEL /F /S "%%F"
But I can't seem to crack it.
%varFolderSource% is a folder path: C:\Temp etc.
I am running this in a Windows 7 batch file.
For /f "delims=" %A in ('dir /b /a-d ^|findstr /i /v /e /r "\.PDF_[a-z0-9]*\.pdf"') do echo %A
Type
for /?
findstr /?
In a batch file use %%A rather than %A at command prompt.

How to create file in partially known folder name using batch file

I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)

Copy the newest file from all the subdirectories

I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.
Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.
for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
You can use the dir command with the /od switch and a for loop:
#echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)
For more help enter help dir in the command prompt.
I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)
Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.
FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y
This worked for me (I tested with .pdf's).

How to copy files. iterating whitespace directories - batch

Hello i want to create a batch file that copies the files from the current directory to another.
for /F "usebackq" %%b IN (`DIR /B /S ""`) DO #(
XCOPY %%b %1
)
So Far so good.
MY problem are the whitespaces in directories.
So when the name of a directory is /Dir whitespaces end/
it does not copy it. "File not found - Dir"
Start bat file with destination
CopyFiles.bat "I:\testFolder*.*"
How can i work around this feature?
Try this:
for /f "delims=" %%b in ('dir /b /s ') do xcopy "%%~b" "%1"
Important is 1) set "delims=" and 2) enclose for loop and other variables in double quotes.

batch script delete file containining certain chars

I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"

Resources