close the batch file command scripts - batch-file

I have the following batch file script code to run. When I run it, it executes successfully, but does not close the cmd window automatically. What should I add to have it close the window automatically?
::Copy Files Made Or Modified Today
#echo off
setlocal
set source=c:\src
set dest=c:\dest
pushd "%source%"
set t=%date:~4%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof

Test this if you are clicking on the batch file.
#echo off
::Copy Files Made Or Modified Today
setlocal
set source=c:\src
set dest=c:\dest
pushd "%source%"
set t=%date:~4%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
cls
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof

Related

Replacing text in a file name

What I am trying to do is replace part of a file name with my computer name.
#echo off
set host=%COMPUTERNAME%
set host=%host:~4, -2%
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do call :next "%%a"
pause
GOTO:EOF
:next
set "newname=%~nx1"
set "newname=%newname:XXXX=zzzz%"
echo ren %1 "%newname%
When I run the above, it replaces the XXXX's with zzzz's
When I change set "newname=%newname:XXXX=zzzz%" to set "newname=%newname:XXXX=%host%"it just deletes the X's.
What happens if you use delayed expansion?
#Echo Off
SetLocal EnableDelayedExpansion
Set "Host=%COMPUTERNAME:~4,2%"
For /F "Delims=" %%A In ('Dir /B/A-D/ON') Do (Set "NewName=%%~nA"
Echo Ren "%%~A" "!NewName:XXXX=%Host%!%%~xA"
Pause
GoTo :EOF

Opening a random folder from a set directory

How can I create a batch script that opens a random folder within a specific directory? This code here prints out a randomly chosen file(I need it to open the folder, not the file) but I could not figure out how to open it.
#Echo Off
:Start
set directory="D:\Movies"
set count=0
for /f %%f in ('dir "%directory%" /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /a-d /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
echo %filename%
pause
goto Start
I suddenly realised what I was doing wrong and solved the problem. Here is the final and working code:
#Echo Off
:Start
set directory="D:\Film"
set count=0
for /f %%f in ('dir "%directory%" /ad /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /ad /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
%SystemRoot%\explorer.exe %filename%
exit /b
goto Start
You really shouldn't need to increment a count and use findstr for such a task; just assigning and sorting a random number should do:
#Echo Off
Set "source=D:\Film"
SetLocal EnableDelayedExpansion
For /D %%A In ("%source%\*") Do Set "$[!RANDOM!]=%%A"
For /F "Tokens=1* Delims==" %%A In ('"Set $[ 2>Nul|Sort"'
) Do Set "target=%%B" & GoTo Found
Exit /B
:Found
Explorer "%target%"
If you wanted a recursive directory search then change line 5 to:
For /D /R "%source%" %%A In (*) Do Set "$[!RANDOM!]=%%A"

Rename multiple files randomly

I created a Menu to call this script that renames multiple text files randomly, it is only working in a .bat file alone. But it is not working in the context of the menu I created, I believe it's something to do with looping, since it only renames the first file! I would like someone to evaluate the situation, thank you very much already.
:4
cls
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"
exit /b
:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
set /a I=!random!%%36
for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
echo if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt
)
pause
goto Menu
)
:5 < - here start the next option of menu
...
Perhaps like this:
:4
cls
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"
pause
goto Menu
:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
set /a I=!random!%%36
for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt
goto :eof
The echo on the exist line is curious. Seems like the ren could error without that.
After reviewing better I see that exit /b is the command that makes the window close after goto: eof it returns to the commands from above. So, i replace exit /b section per:
pause
goto Menu
and now it return to my Menu
:Menu
Complete Code:
cls
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F"
pause
goto Menu
:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
set /a I=!random!%%36
for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
if exist !name!.txt goto :retry
endlocal & ren %1 %name%.txt
goto :eof

cmd does not support unc paths as current directories pushd

i was trying to copy a files modified today from one shared drive another shared drive.
but when i execute the batch file, it displays the following error.
cmd does not support unc paths as current directories
the script is as below
#echo off
Set PhotosrcPath=\\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO\
Set PhotodestPath=\\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO\
::Copy photoFiles from server1 to server2 Made Or Modified Today
#echo off
set source=%d%%PhotosrcPath%
set dest=%d%%PhotodestPath%
pushd "%source%"
set t=%date:~4%
echo %t%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
goto :eof
popd
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" Xcopy /y %1 "%dest%"
goto :eof
net use X: \\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO
net use Y: \\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO
::Copy photoFiles from server1 to server2 Made Or Modified Today
#echo off
set source=X:\
set dest=Y:\
pushd "%source%"
set t=%date:~4%
echo %t%
for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
popd
net use Y: /d /y
net use X: /d /y
goto :eof
:PROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" Xcopy /y %1 "%dest%"
goto :eof
Something like that might work. Look up NET USE for more information.
I'm curious why you aren't just using XCOPY - I would think that the /D command might do what you need. I would throw in a /M too.
xcopy \\hqcp-appsvr01\Files\ApplicationDocuments\AppDocs\49\PHOTO\*.* \\hqcp-appsvr02\Files\ApplicationDocuments\AppDocs\49\PHOTO\*.* /y /d /m
There is no reason to use pushd. The following modification of the for loop may be enough.
for /f %%a in ('dir "%source%" /b /a-d /o-d') do call :PROCESS "%%a"

Batch File - Recursively check folders and move files

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

Resources