Hello I was told to ask another question
I would like to see how to add this line to my current script
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO (
Or if I can get the help updating "File" and "Filename" to this (1) *.txt this would great as well
#ECHO OFF
SETLOCAL
ECHO STARTING 1
For %%G In ("%~dp0..\New Folder 1") Do Set "source=%%~fG"
For %%G In ("%~dp0..\New Folder 2") Do Set "target=%%~fG"
For %%G In ("%~dp0..\New Folder 3") Do Set "destdir=%%~fG"
set "FILE=(1) Homes in Texas.txt"
set "FILENAME=(1) Homes in Texas.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%source%\%file%"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "opcn"^<"%source%\%file%"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(A)_sets.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "paid"^<"%source%\%file%" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "whbn"^<"%source%\%file%" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(B)_sets.txt"
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
If Exist "%source%\(2) *.txt" (
call "%~dp0(2) Extraction tool.bat"
)
GOTO :EOF
Now I would like to keep this script the speed on my script is very fast it can edit my files at 1 sec per file
I'm OK with using a partial Filename
Because of my setup it makes for no errors, it's super fast speed and all files have these in the filename (?)
With the way I have my setup my workstation, I would have to edit my scripts over and over to match the file name, so I'm trying to use these (1) *.txt, because all my txt file look like this
Sub Houston Folder
(1) Houston.txt
(2) Houston.txt
(3) Houston.txt
Sub Auston Folder
(1) Austin.txt
(2) Austin.txt
Sub Dallas Folder
(1) Dallas.txt
(2) Dallas.txt
(3) Dallas.txt
I have over 5000 files easy
They all have (?) in the file name so looking for (1) *.txt, but don't know if this will be an issue with ren "%destdir%\(1).txt" "%filename%" using (1) *.txt
If there is a way to fix my File and Filename to work on (1) *.txt and to keep the original file name as end result that would be very appreciated
Any Help will be great
Is this correct, when I set this up this way or any other way I get blank files [corrected code below. Stephan]
REM not here... set "FILE=%~1"
set "FILENAME=(1) Homes in Texas.txt"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO call :label "%%q"
goto :eof
:Label
REM here "%~1" gets the value "%%q" from the CALL command, a FQDN (filename including drive and path because of 'dir /s /b')
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%~1"') do set /a start=%%a
My Setup Folders
Main Folder
|Extraction Batch Folder
|New Folder 1
|all original txt files
|New Folder 2
|all extracted txt files
|New Folder 3
|all renamed and merged files
It's now almost ready We got File fixed with that line
still working on Filename
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
I know that wildcards does work with ren I tested this to confirm *
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "(*)A.txt"
and in %destdir% it rename to (1)A.txt so the wild card sees the (1)
how can I set this up to take the matching (1) on the filename located in the source folder and use that to rename the (1).txt found in `%destdir% folder
Related
So I have a folder that processes the containing files automatically.
I have another folder that has thousands of files to be processed and I created a script that moves 100 files to that processing folder every hour with Task Scheduler, but I want to do it only if that destination folder has less than 20 files in it.
Can someone help me?
Thanks
#echo off
set Source=Source folder
set Target=Destination folder
set MaxLimit=100
for /f "tokens=1* delims=[]" %%G in ('dir /A-D /B "%Source%\*.*" ^| find /v /n ""') do (
move /y "%Source%\%%~nxH" "%Target%"
if %%G==%MaxLimit% exit /b 0
)
Do a simple count on files in the destination folder to determine if processing should occur using a condition check.
Set "Target=Destination folder"
Set count=0
FOR %%a IN (%Target%\*.*) DO (
Set /a count+=1
)
If %count% LSS 20 (
GOTO process
) else (
EXIT
)
:process
"Insert your Move command Here"
Count the files in the target location and exit if there are enough
#echo off
set Source=Source folder
set Target=Destination folder
set MaxLimit=100
for /f %%a in ('dir /b /a-d "%target%\*" ^|find /c /v ""') do set "remaining=%%a"
if %remaining% geq 20 (
echo enough files in target; nothing to do.
goto :eof
)
for /f "tokens=1* delims=[]" %%G in ('dir /A-D /B "%Source%\*.*" ^| find /v /n ""') do (
move /y "%Source%\%%~nxH" "%Target%"
if %%G==%MaxLimit% exit /b 0
)
Or count the files in the destination folder and only process if there are less than your defined quantity:
#Set "SrcDir=Source folder"
#Set "DstDir=Destination folder"
#Set /A "MaxCnt=100,MinCnt=20"
#For /F %%A In ('""%__AppDir__%xcopy.exe" "%DstDir%\*" ? /LSHQ 2>NUL"'
)Do #If %%A Lss %MinCnt% For /F "Tokens=1*Delims=[]" %%B In (
'"Dir /B/A-D/O-D "%SrcDir%"|"%__AppDir__%find.exe" /V /N """'
)Do #Move /Y "%SrcDir%\%%~nxC" "%DstDir%">NUL&If %%B Equ %MaxCnt% Exit /B 0
echo off
for /d /r "c:\" %%a in (TemporaryFolder) do if exist "%%a" echo Removing %%a & rmdir /s /q "%%a"
This for loop deletes every directory called "TemporaryFolder" from the C:\ drive. How would I go through every mounted volume (ie. A:\ - Z:) to delete the directory "TemporaryFolder"?
Edit (This test didn't work):
#echo off
echo Deleting Temporary Folders... Please be patient.
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul & echo Deleting %%B
echo Successfully deleted.
pause
Based upon my comment, here's an example of a nested For loop, which should do as asked:
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%ATemporaryFolder" 2^>Nul') Do RD /S/Q "%%B" 2>Nul
The outer For loop runs the MountVol command which returns the mounted drive paths as %%A. The nested For loop performs the recursive directory search for those named TemporaryFolder located within %%A.
Edit
Here's a multiline version of the same routine with added messages:
#Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In ('MountVol^|Find ":\"') Do (
Echo Deleting directories named %objFolder% from %%A
For /F "Delims=" %%B In ('Dir /B/S/AD-S-L "%%A%objFolder%" 2^>Nul') Do (
Echo Deleting %%B
RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
)
)
Pause
You should only modify the objFolder name on line 2.
Instead of MountVol, this one uses PowerShell to get the drives, and matches System and Reparse Points too, (take account that you'd need appropriate permissions to remove system directories):
#Echo Off
Set "objFolder=TemporaryFolder"
Echo Please be patient...
For /F "Tokens=*" %%A In (
'PowerShell -NoP "GDr -P FileSystem|?{!$_.Used -Eq ''}|Select -Exp Root"'
) Do (Echo Deleting directories named %objFolder% from %%A
For /F "Delims=" %%B In ('Dir /B/S/AD "%%A%objFolder%" 2^>Nul') Do (
Echo Deleting %%B
RD /S/Q "%%B" 2>Nul && Echo Successfully deleted %%B
)
)
Pause
Thanks in advance for any help given.
After searching through all relative threads and google search I'm stumped on finding a solution to output a variable name for merging two PDF's.
So I have 100's of PDF's I need to combine (two at a time) in a folder c:/test
The files are set out like below
Company Name Invoice No 123456
Company Name Invoice No 123456 details
Now I have managed to move two files at a time to a different folder and merge them but can't seem to get the desrired output name I'm after which is to put a week ending date in front (or at the end, not fussed) of the first merged filename. Below is the code I have thus far which works but the output file name is blank but gets created.
Very new to batch scripting and would appreciate any help :)
#echo off
setlocal enableextensions enabledelayedexpansion
set pdftk=C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe
set Source=C:\test
set Target=C:\test\test2
set num=2
set filenumber=1
for /F "tokens=1,2 delims=:" %%f in ('dir /b /a-d "%source%\*.pdf" ^| findstr /n "^" ') do (
if %%f leq %num% (
copy "%source%\%%g" "%target%" /y > nul
) else goto endCopy
)
:endCopy
endlocal
for /F "tokens=1,2 delims=:" %%f in ('dir /b /a-d "%target%\*.pdf" ^| findstr /n "^" ') do (
if %%f leq %filenumber% ( set file=%%~nA
)
)
pdftk *.pdf cat output we_19_9_2017_%file%.pdf
In endCopy you are trying to get the name of A whereas you are iterating with f. Use set file=%%~nf to set the name of file or set file=%%~ng for second file.
And move endlocal at the end to expand !file! at the end of script like this (note the !):
:endCopy
set "cmd=dir /b /a-d "%target%\*.pdf" ^| findstr /n "^""
for /F "tokens=1,2 delims=:" %%f in ('%cmd%') do if %%f leq %filenumber% set file=%%~nf
pdftk *.pdf cat output we_19_9_2017_!file!.pdf
endlocal
Read more about DelayedExpansion at: https://ss64.com/nt/delayedexpansion.html
The last command doesn't use the target folder for the input files and thus looks for the input files in the current folder, so either include the path or first change to the target path.
Also you set a path-variable for pdftk but don't use it.
If this path isn't included in the %path% it can't be find.
Try this (untested)
#echo off
setlocal enableextensions enabledelayedexpansion
set "pdftk=C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
set "Source=C:\test"
set "Target=C:\test\test2"
set num=2
set filenumber=1
for /F "tokens=1,2 delims=:" %%f in (
'dir /b /a-d "%source%\*.pdf" ^| findstr /n "^" '
) do if %%f leq %num% (
copy "%source%\%%g" "%target%" /y > nul
) else goto endCopy
:endCopy
endlocal
for /F "tokens=1,2 delims=:" %%f in (
'dir /b /a-d "%target%\*.pdf" ^|findstr /n "^" '
) do if %%f leq %filenumber% set file=%%~nf
PushD "%Target%"
"%pdftk%" *.pdf cat output we_19_9_2017_%file%.pdf
PopD
Is it possible to do this?
In a folder i have files with same initial names
Example:
Main folder
-Quest2323231.txt
Quest2343434.txt
Quest2343435.txt
Fund103.txt
Fund102.txt
I have a config file (abc.config) in which i have name of file on which i need to count and move them . If the count is more than 2 then i need to move them.
In this case for e g I need to find files which have name as 'Quest'
Appreciate you help on this.
#echo off
setlocal enableextensions
set "number="
for /f "tokens=1" %%a in (
'dir /a-d /-c "c:\mainfolder\quest*" 2^>nul^|findstr /b /c:" "'
) do if not defined number set "number=%%a"
if not defined number set "number=0"
echo %number%
Untested: This expects text in the first line of abc.config which is the file information such as Quest in the example and if there are more than 2 matching files in the source folder then it will echo a move command to move them to the target folder.
Change *%file%* to %file%* in two places if you want to match only the start of the filename.
Remove the echo to actually perform the move commands.
#echo off
set "source=c:\mainfolder"
set "target=d:\target\folder"
set /p "file=" < "abc.config"
for /f %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" 2^>nul^|find /c /v "" ') do set "number=%%a"
if %number% GTR 2 for /f "delims=" %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" ') do (
echo move "%source%\%%a" "%target%"
)
pause
I am trying to create a windows batch file that will scan a folder with many sub folders. Each sub folder can contain many files. I need the script to check if a sub folder contains over a certain number of files, and if it does move half of the files to a new folder with the same name but with a number at the end.
Example:
Main folder
-Subfolderone
-Subfoldertwo
-Subfolderthree
If Subfoldertwo contains over a certain number of files, lets say 1000, then half of the files within Subfoldertwo will be moved to Subfoldertwo(2), and so on for each sub folder.
Main folder
-Subfolderone
-Subfoldertwo
-Subfoldertwo(2)
-Subfolderthree
Any help would be much appreciated. Thank you.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET limit=5
FOR /f "delims=" %%a IN ('dir /b /s /ad "%sourcedir%\*"') DO (
SET /a newnum=2
FOR /f %%c IN ('dir /b/a-d "%%~a" 2^>nul ^|find /c /v ""') DO IF %%c gtr %limit% CALL :process "%%a"
)
)
GOTO :EOF
:process
IF EXIST "%~1(%newnum%)\" SET /a newnum+=1&GOTO process
ECHO MD "%~1(%newnum%)"
FOR /f "skip=%limit%delims=" %%m IN ('dir /b /a-d "%~1"') DO ECHO MOVE "%~1\%%m" "%~1(%newnum%)\"
GOTO :eof
Simple enough. I've set the sourcedir to a constant for my testing and the limit to 5 for the same reason.
First build a list of the original diretory tree, then count the files in each directory. If that count is greater than the limit, process the directory.
In process, first find whether the proposed new directory already exists. If it does, keep incrementing the number 'til it doesn't.
Then list the filenames (only) from the original full-directoryname, skipping the first %limit% and for the remainder, move them to the new directoryname.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
AND change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
Edit : revised for 'move half the files'
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET limit=5
FOR /f "delims=" %%a IN ('dir /b /s /ad "%sourcedir%\*"') DO (
SET /a newnum=2
FOR /f %%c IN ('dir /b/a-d "%%~a" 2^>nul ^|find /c /v ""') DO IF %%c gtr %limit% SET /a nmove=%%c / 2&CALL :process "%%a"
)
)
GOTO :EOF
:process
IF EXIST "%~1(%newnum%)\" SET /a newnum+=1&GOTO process
ECHO MD "%~1(%newnum%)"
FOR /f "skip=%nmove%delims=" %%m IN ('dir /b /a-d "%~1"') DO ECHO MOVE "%~1\%%m" "%~1(%newnum%)\"
GOTO :eof
(simply calculate half of the count into nmove then skip that number instead)
you might test this:
#ECHO OFF &SETLOCAL
set "StartFolder=X:\Main folder"
set /a MaxFiles=1000
cd /d "%StartFolder%"
:NewFolderCreated
set "NewFolderFlag="
for /f "delims=" %%a in ('dir /b /ad /on') do call:process "%StartFolder%\%%~a"
if defined NewFolderFlag (goto:NewFolderCreated) else goto:eof
:process
SETLOCAL
cd "%~1"
for /f %%b in ('dir /b /a-d 2^>nul^|find /c /v ""') do set /a FileCount=%%b
if %FileCount% leq %MaxFiles% exit /b
set /a MoveCount=FileCount-MaxFiles
set "CurrentFolder=%~n1"
set "NextPath=%StartFolder%\%CurrentFolder%(2)%~X1"
echo("%CurrentFolder%"|findstr /re ".*([0-9][0-9]*)\"^">nul||goto:moving
set "BasePath=%CurrentFolder:~0,-1%"
:loop
if not "%BasePath:~-1%"=="(" set "FolderNo=%BasePath:~-1%%FolderNo%"&set "BasePath=%BasePath:~0,-1%"&goto:loop
set /a FolderNo+=1
set "NextPath=%StartFolder%\%BasePath%%FolderNo%)%~X1"
:moving
echo(Moving %MoveCount% files from "%~1" to "%NextPath%".
md "%NextPath%" 2>nul &&set "NewFolderFlag=true"
for /f "skip=%MaxFiles%delims=" %%b in ('dir /b /a-d /o-n') do move "%~1\%%~b" "%NextPath%" >nul
endlocal &set "NewFolderFlag=%NewFolderFlag%"
exit /b