Batch Create Folders and Relocate Files Based on Partial Filename - batch-file

I need to create folders and relocate files to the folders based on the first eight characters of the filenames. The files will look like:
GG001652 - 5211_Infoscitex.xls
GG001652 - 5211- as of 7.31.12.pdf
GG001570 - 7575 FSR (3.31.2010).pdf
GG001570 - 7575_IC_6.30.12.xlsx
GG001570 - 7575_SF 425_6.30.12.xls
I'd like the batch to create two folders:
GG001622
GG001570
Each containing the files beginning with those 8 characters. File length and nomenclature is inconsistent, but the first 8 characters are standardized, and file types include .pdf, .doc, .docx, .xls, .xlsx and .msg
Spent a good few hours trying to modify Magoo's code in this post, but couldn't for the life of me get it to work:
Batch create folders based on part of file name and move files into that folder
Have a work deadline I need to meet, so greatly appreciate any help offered.

If the first 8 chars are always followed by space,
use a for /f to split the name with default delimiter space and token 1. See ss64.com/nt/for_f
#Echo off
for /f "delims=" %%A in (
'DIr /B /A-D "* - *.*" ^| findstr /I "^[PG]G[0-9][0-9][0-9][0-9][0-9][0-9].-..* '
) Do for /f %%B in ("%%A") do (
MD %%B >NUL 2>&1
Move "%%A" %%B
)
The first iterating for is now replaced with a more complex for /f parsing dir output wihich is filtered by a findstr with a RegEx to match the described name structure.
a P or G followed by a G followed by 6 numbers.

Related

Delete string in multiple files in directory with cmd

:-)
I'm very new to cmd-commands and .bat-stuff, so would be grateful if any of you could help me with a problem.
So...I have a folder with 116 .txt files.
All these .txt files contain lines starting with a "#" and some other lines starting with "---".
I figured that findstr /v /L /C:"#" test.txt > test2.txt works in creating a new file without any lines that contain "#"
Now my question, is it possible to write something like findstr /v /L /C:"#" & "---" *.txt > *.txt ?
The goal is that each existing file is overwritten and all lines containing either "#" or "---" are removed.
Glad for any help!
Cheers,
Dyz
#ECHO OFF
SETLOCAL
rem The following setting for the source directoryis a name
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR /f "delims=" %%w IN ('dir /b /a-d "%sourcedir%\*.txt"') DO FINDSTR /v /L /c:"#" /c:"---" "%sourcedir%\%%w">"%sourcedir%\tempfile"&MOVE /y "%sourcedir%\tempfile" "%sourcedir%\%%w" >nul
GOTO :EOF
The meat of the matter is the for/f line, obviously.
Run dir /b to obtain a list of the filenames. /a-d excludes directorynames. This list is built in memory first, as the .txt files are being replaced - this ensures that the files are only "listed" once.
The for /f "delims=" reads that list and assigns each line-contents (filename+extension only) to %%w. The delims= ensures that filenames containing spaces are properly processed.
Then execute the findstr, which can take multiple /c: arguments to or the strings.
[I haven't tried it, but FINDSTR /v /L "# ---" "%sourcedir%\%%w" should produce identical processing]
The output of the findstr is directed to a temporary filename (I chose to put it in the source directory, but it could be anywhere with any name) and then the temporary file is moved over the original with the 1 file moved message suppressed by a >nul.
As always, test first on a copied sample.

Choose Random text file in batch

is it possible to make a batch file choose a totally random text file from within a specified folder
and can somebody help me make it?
example
lets say that i got a folder located here: C:\Workstation\Jobs
and in that folder there is maybe 10-20 .txt files at a time
the goal here is to make that batch file check the folder and then choose a random file and output the choosen file to a variable
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f %%a IN (
'dir /b /a-d "%sourcedir%\*.txt"^|find /i /c ".txt" '
) DO SET /a selection=1 + (%RANDOM% %% %%a)
FOR /f "tokens=1*delims=:" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt"^|findstr /n /i ".txt" '
) DO IF %%a==%selection% SET filename=%%b
ECHO selected %filename%
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
find /c counts the number of files found by the dir command and the for assigns the count to %%a. selection is then assigned to 1+(randomnumber mod filecount) giving 1..filecount.
findstr /n outputs the name of each file found by the dir command, prefixed by a sequential number : %%a will be assigned the number (token before the : and %%b the filename (afther the first delimiter where delims is :)
When the line number matches selection, filename is assigned the filename found.
In the case of the said folder only containing text files, you'd need the following:
a list of folder contents in some sort of array (dir)
count the amount of files (for loop)
pick a random number between 0 and the amount of files (%RANDOM%*amount_of_files/32768)
output the matching filename (in the array) to the variable you want
Hope this pushes you in the right direction :-)
sources:
http://ss64.com/nt/syntax-random.html
How to loop through files matching wildcard in batch file
Create list or arrays in Windows Batch

windows - scan filenames and move files to dirs and subdirs with names derived from filenames

I (unfortunately) have thousands of files which need to be reorganized into different folder scheme.
All my files follow the pattern:
d:\mainfolder\0001.pic\AMY_CAT_file1.jpg
d:\mainfolder\0001.pic\EVE_CAT_file1.jpg
d:\mainfolder\0002.pic\AMY_BIRD_file2.jpg
d:\mainfolder\0002.pic\EVE_BIRD_file2.jpg
what I'm looking for is a *.bat file that could move the files into folders that go like this:
...\CAT_\AMY\0001.pic\AMY_CAT_file1.jpg
...\CAT_\EVE\0001.pic\EVE_CAT_file1.jpg
...\BIRD\AMY\0002.pic\AMY_BIRD_file2.jpg
...\BIRD\EVE\0002.pic\EVE_BIRD_file2.jpg
in other words, the files would have to be moved:
into the subsubfolder named after source directory of a file
which is inside the folder named after 1st to 3rd
symbol, and finally
which is inside the folder which is named after the 5th to 8th symbol in the filename
I realise it might sound confusing, any help is greatly appreciated.
1st to 3rd symbol and 5th to 8th symbol is a bad idea, as the names might be of different lengths (see cat, bird, and possibly elephant; or Eve, Amy, and possibly Stephan), but you have there a nice underscore that can serve as delimiter:
#echo off
setlocal EnableDelayedExpansion
cd /d "d:\mainfolder"
for /f "delims=" %%F in ('dir /s /b *.jpg') do (
for /f "tokens=1,2,* delims=_" %%A in ("%%~nF") do (
set folder=%%~dpF
for %%X in (!folder:~0^,-1!) do (
ECHO move "%%F" "...\%%B\%%A\%%~nxX\%%~nxF"
)
)
)

How to make a batch script to create a folder from only numbers in file names

I am trying to create a batch script on windows 7 to do the following:
I have a folder which another program dumps files to.
All the files have numbers in their name along with other identifying information.
I would like to have a batch script search for all the digits in the file name and then create a folder (folder name is just the digits) and move all files that contain those digits to the folder.
It must be applicable to numerous users in my office.
currently i have a very bad primitive system running which doesn't account for mistakes:
#echo off
setlocal enabledelayedexpansion
pushd "%USERPROFILE%\Desktop\RawFiles"
for %%a in (*) do (
set fldr=%%~na
set fldr=!fldr:~0,5!
md "!fldr!"
move "%%a" "!fldr!"
)
popd
if not exist "%USERPROFILE%\Dropbox\agents" mkdir "%USERPROFILE%\Dropbox\agents"
SET "src_folder=%USERPROFILE%\Desktop\RawFiles"
SET "tar_folder=%USERPROFILE%\Dropbox\agents"
for /f %%a IN ('dir "%src_folder%" /b') do move /-y %src_folder%\%%a %tar_folder%
exit
the files in the "rawfiles" folder are as follows:
12345 - tech pack.pdf
12345.pdf
12345-artwork.AI
"#12345- artwork.AI"
What i created only works for perfect instances where the numbers come first in files and are exactly 5.
Unfortunately there is a lot of human error here so this solution is not viable.
an example of a mistake is an accidental 6 digit number or even putting the "#" sign before the numbers
I also would like to be able to override the files in the destination folder by running this twice but am getting "access denied".
Please help!
thanks
The Batch file below extract the digits from the file names as requested (maximum 6 groups of digits separated by other characters, this may be modified), so you may manipulate they in any way you wish; the ECHO commands are just examples. Note that the space must be the last character in eliminate variable.
#echo off
setlocal EnableDelayedExpansion
set "eliminate=#-abcdefghijklmnopqrstuvwxyz "
for %%a in (*.*) do (
for /F "tokens=1-6 delims=%eliminate%" %%b in ("%%~Na") do set folder=%%b%%c%%d%%e%%f%%g
if not exist "!folder!" ECHO md "!folder!"
ECHO move "%%a" "!folder!"
)

Extracting A List Of Phrases From A Large TXT File, And Writing A Single TXT File With Each Phrase (Also the title of that file)

I have a list of terms, and I have large text files containing some of those terms. My task is to come up with one text file for each of the terms in the same directory as the original text file.
BEFORE
listofterms.txt (apple cores, peaches, roast beef, things wrapped in nori, etc...)
LargeFileOfFoodWords.txt (ranging from 20-20,000 individual lines of non-duplicated text)
AFTER
apple cores.txt, peaches.txt, roast beef.txt, things wrapped in nori.txt (etc...)
LargeFileOfFoodWords.txt (original file, unchanged--or if possible, with all 'listofterms' extracted)
With the following bat file, I am able to do this with a SINGLE word. However the resultant file contains the paths of ALL files within the directory the batch file resides in. Successive searches also include apple cores.txt, peaches.txt, etc...
So I have some elements working, and need to tweak the following to:
not search any files but those I choose
include ONLY extracted text without metadata
continue searching and writing files until reaching the bottom of my list of search terms
This script will work with any .txt file and give you the resultant "SearchTerm.txt," if you wish to test it yourself.
#echo off
set RESULT_FILE="result.txt"
set /p "buck1=Enter Bucket Word or Phrase to find:"
pushd %~p0type NUL > %RESULT_FILE%.tmp
for /f "delims=" %%a in ('dir /s/-b/l *.txt')
do (for /f %%c in ('find /i /c "%buck1%" "%%a"')
do (for /f "tokens=*" %%f in ('find /i "%buck1%" "%%a"')
do if %%c neq 0 echo :`%%f))>>"%RESULT_FILE%".tmp
move %RESULT_FILE%.tmp %buck1%.txt >nul 2>&1
popd
this might work for you:
for /f "delims=" %%a in ('findstr /xg:listofterms.txt LargeFileOfFoodWords.txt') do (type nul>"%%~a.txt"&echo(%%~a>"result.txt")
For more help enter help for and help findstr.

Resources