Batch file to move files based on name [duplicate] - batch-file

This question already has answers here:
Order and move files into directories based on some filenames pattern
(2 answers)
Closed 5 years ago.
I currently have over 100.000 files (.bak) which I need to move 1000 files to a different directory. The files that need to be moved have a twin-file with a different extension (.xml) so finding them should be easy, but for the life of me I can't figure out how to do this. I have no experience with .bat-files and I've already been struggling with stuff for a day. Can someone help me out please?
Quick example:
First file:
File1thatneedstomove.bak
File1thatneedstomove.xml
File1thatdoesntneedstomove.bak
File2thatdoesntneedstomove.bak
File3thatdoesntneedstomove.bak
File2thatneedstomove.bak
File2thatneedstomove.xml
So I need to move the 1st and 6th file to a different folder because they have a twin file where just the text behind the period is different.

Not tested:
set "source_dir=C:\baks"
set "destination=C:\dest"
for %%a in ("%source_dir%\*bak") do (
if exist "%%~dpna.xml" (
echo move /y "%%~fa" "%destination%"
)
)
It will echo the needed move command parameters. If it is ok remove echo word in the line in the brackets.

Related

using windows batch script to automate unzip task [duplicate]

This question already has an answer here:
Automating task for unzing a gz compressed file using windows batch script [closed]
(1 answer)
Closed 2 years ago.
I have two issues with this code I have attached below,
it doesn't delete the original file after unzip, I want the script to delete the original file from source folder
the converted file is not saved in target folder as set but it creates a tree of the directory in the same folder of the script and saved output there.
Please help me solve this issue
Sample code I have tried
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Please help me to write the script as described above
try: del file /f /q
try to enclose -o parameter with a quotes: "-o%target%"

Adding tags to the beginning and end of the filename is stuck in a loop when there are more than 500 files in a folder- How to avoid this? [duplicate]

This question already has an answer here:
At which point does `for` or `for /R` enumerate the directory (tree)?
(1 answer)
Closed 3 years ago.
We receive files from an organization and the filename is typically in numbers: 123456789.dat and I have a batch script that will add a prefix and a new suffix to the filename, IE:
COMPANY-1-123456789.dat.txt
The code works exactly as it should, BUT when there are over 500 files in the folder, the append function will keep adding the prefix and suffix many times over and over and it would not stop, IE:
COMPANY-1-COMPANY-1-COMPANY-1-COMPANY-1-123456789.dat.txt.txt.txt.txt
I had to kill the batch script and split up the number of files into two smaller groups and that would work just fine. Unfortunately, I won't always be around to catch this problem and would like to prevent this from happening again.
Did exhaustive search over the internet but could not find anything remotely similar to what I am looking for. If this problem exists elsewhere, would you please point me to the question? It would be greatly appreciated.
We have several different companies that send us numerous files on a daily basis so the code I pasted would have at least 10 different companies involved; it is basically a repeat of the below snippet all on one batch file. Only one company is sending us more than 500 files and I would need to manually split them up into groups of two in order to get the code working properly. Anything over 500 would just get caught in a loop. Any suggestions would be greatly appreciated!
CHDIR /D "v:\DIR\COMPANY_NAME\ORIGFILE"
for %%a in (*.*) do ren "%%a" "COMPANY-1-%%a.TXT"
XCOPY /Q /Y "*.TXT" "C:\Local_dir\Sub_Folder\"
MOVE /Y "*.TXT" "COMPLETED\"
The simple FOR command does not scan the entire directory before iterating. Rather it scans until some buffer is full, processes that buffer, and then tries to pick up scanning where it left off. But if you have renamed files, it can confuse the scanning process - the rename operation can change where the file appears in the folder listing such that the resumed scan picks up an already renamed file.
This problem can only arise when the number of files exceeds the internal FOR buffer.
The fix is to use FOR /F with DIR /B instead, as that buffers the entire result of the DIR command before it begins iterating.
for /f "delims=" %%a in ('dir /b /a-d-h-s *.*') do ren "%%a" "COMPANY-1-%%a.TXT"

Batch file to split text file into multiple files in same directory on Win Svr 2012 using NO 3rd Party Apps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am hoping the wonderful experts here can assist me with my problem. I have found this site extremely useful in the past in solving other issues I have had but I have searched this site and tried all the answers similar to my problem but cant seem to get any of them to work for this particular problem. I am not a coder but have dabbled with other code and got them to work for other issues.
I need to split a text file containing keywords(1 kw per line, no blank lines) into multiple text files within the same directory, each with 300 lines (except last text file if total input lines not exactly divisible by 300). The input file will NEVER be larger than 100MB.
Ideally, I then need the input file deleted once it has been split and all split text files moved to another directory (there are no other text files to worry about in the original directory)
I need it to be a bat file or vbs script called via bat file.
input file:
keyword-file.txt
output files:
keyword-file_1.txt (300 lines)
keyword-file_2.txt (300 lines)
keyword-file_3.txt (300 lines)
etc
To clarify requirements the above:
Split input text file (<100MB) into smaller text files, each with 300 lines
Delete input text file
Move all split text files to another specified directory
For the splitting, perhaps something like this will do:
#Echo Off
Setlocal EnableExtensions DisableDelayedExpansion
Set "inFile=keyword-file.txt"
Set "nLines=300"
If Not Exist "%inFile%" GoTo :EOF
For %%A In ("%inFile%") Do Set "fName=%%~nA"&Set "fExt=%%~xA"
Set "count=0"
For /F %%A In ('Find /C /V ""^<"%inFile%"') Do Set "fLines=%%A"
(For /L %%A In (1 1 %fLines%) Do (Set/P "data="
Set/A "file=(count/%nLines%)+1", "count+=1"
SetLocal EnableDelayedExpansion
(Echo=!data!)>>"%fName%_!file!%fExt%"
EndLocal))<"%inFile%"
EndLocal
GoTo :EOF
I'll leave the deletion and move commands to you…

Determine if given variable is a directory [duplicate]

This question already has answers here:
How to test if a file is a directory in a batch script?
(23 answers)
Closed 6 years ago.
I have a batch file script that needs to verify if the directory that the user enters in is a real directory or path so that I can create it if it dose not exist.
:GetDir
set /p Dir=
if not <is directory?> %Dir% goto:GetDir
if not exist %Dir% mkdir %Dir%
How can I determine if the value given by the user is a directory?
There are millions of things that the user could enter in that is obviously not a directory.
notes for editor:
This note is to be removed after the editor reads it. I apologize ahead of time if I was not supposed to add this note in. Please correct me so I don't do this again if this is the case and I would appreciate being told where to add these types of notes if a similar situation comes up again. I am trying to maintain and recover this question from when I originally asked it in order to fix my bad reputation for asking bad questions on here. This is not a duplicate question because the marked duplicate is in a different scripting language that I am unfamiliar with. I would also like to mention that I did to some heavy research before asking this question on the the issue that I was having. It would also be greatly appreciated if answering this question could be enabled again so that I can answer my own question because I am not sure that anybody else is going to have the answer, and I currently know how to answer it.
How do I test if a file is a directory in a Batch script ?
#echo off
set VAR="C:\Program Files"
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO %VAR% It's a directory
pause
You can use this to test if it is specifically a directory (and not a file)
#echo off
set "file=C:\someDirectory"
if exist "%file%\." echo directory
pause
Here's a way (two ways, really) to determine if a directory exists, and that it's actually a directory and not a file.
set /p "ServerMods=Enter a Directory: "
REM First, make sure the input ends with a backslash (\)
if %ServerMods:~-1% NEQ "\" set "ServerMods=%ServerMods%\
REM Option 1
if not exist %ServerMods% goto :GetDir
REM Option 2
dir %ServerMods% 1>nul 2>nul || (goto :GetDir)
if exist "%ServerMods%\." goto GetDir

How to make a batch file put every file name in a folder in a different variable with for loop [duplicate]

This question already has answers here:
DIR output into BAT array?
(2 answers)
Closed 7 years ago.
There is a folder called 'weapons', and in this folder are empty files, like 'P250.wep, AWP.wep'. I need to make a batch file that loops through the folder, takes every file name, and puts it in a different variable. For example, after looping these files, P250.wep should be stored in variable weapon1, and AWP.wep in weapon2. Please help me!!!
You can try this method, but it's automatically sort alphabetically before the loop starts. So, this mean AWP.wep will be weapon1, and P250.wep will be weapon2. One more thing, I don't know which one do you want to store, you mentioned "take every file name", but you also mentioned file name with extension... If you don't mind, take a look at the script:
#echo off
Setlocal EnableDelayedExpansion
set count=1
for %%a in (c:\PATH\weapons) do (
set "weapon!count!=%%~na"
set /a count+=1
)
pause >nul

Resources