Moving text files one by one using batch-file with a timeout - batch-file

hi guys i'm a trying to move text files one by one with a timeout of 8 seconds from one folder to another using a batch script. i have this script so far;
move /-y "D:\example\original\*2007*.txt" "D:\example\New folder\"
what should i add so that it doesn't move the files at once?

As #npocmaka recommend to you the user confirmation for example like this batch script : Movies wrapper script
#ECHO OFF
SETLOCAL
SET "ROOT=%~dp0"
SET "FORCE=0"
IF /I "%~1"=="/f" (
SET "FORCE=1"
SHIFT
)
IF "%~1"=="" GOTO :END_PARSE
SET "ROOT=%ROOT%%~1\"
:END_PARSE
ECHO -----------------------------------------------
ECHO WRAPPER - "%ROOT%"
ECHO -----------------------------------------------
SET "COUNT_SUCC=0"
SET "COUNT_FAIL=0"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
FOR %%f IN ("%ROOT%\*.*") DO (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
IF !FORCE! NEQ 1 (
rem echo force=!Force!
SET /P "INPUT=Do you want to wrap the file "%%~nxf" ? (Y/[N])"
IF /I "!INPUT!"=="Y" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\">NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\">NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
) Else (
REM Checks if the file isn't the batch file.
IF NOT "%%~ff" == "%~f0" (
REM Create a directory as the same name.
IF NOT EXIST "!ROOT!\%%~nf\" MD "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Checks if the directory was created.
REM /Y Suppresses prompting to confirm you want to overwirte an existing destination file.
IF EXIST "!ROOT!%%~nf\" MOVE /y "%%~ff" "!ROOT!%%~nf\"
rem >NUL 2>NUL
REM Count files who has been wrapped or not.
ECHO.
IF NOT EXIST "!ROOT!%%~nf\%%~nf%%~xf" (
RMDIR /s /q "!ROOT!\%%~nf\" >NUL 2>NUL
SET /a "COUNT_FAIL+=1"
ECHO The file "%%~nf" hasn't been wrap.
) ELSE (
ECHO The file "%%~nf" has been wrapped.
SET /a "COUNT_SUCC+=1"
)
)
)
)
)
ECHO.
SET /a "COUNT_TOT=COUNT_SUCC+COUNT_FAIL"
ECHO Total of %COUNT_TOT% files(s) : %COUNT_SUCC% file(s) wrapped and %COUNT_FAIL% file(s) failed.
ECHO.
PAUSE

use a simple for loop to process each file individually:
for %%a in ("D:\example\original\*2007*.txt") do (
move /-y "%%~fa" "D:\example\New folder\"
timeout /t 8
)
For more information, see for /?

Related

Batch Script to find a files inside all Sub Folders

I have the multiple images in sub folder, I don't know how many folders are there. I want to Batch script to find listed names in all the folders and copy the images to destination folder. I tried below script but am getting file not found error.
#echo off
rem Find files and copy files
setlocal EnableExtensions EnableDelayedExpansion
set "SourceBaseFolder=D:\System backup\picture batch file\Test\15oct2015"
set "TargetBaseFolder=C:\OutputFolder"
if not exist "%SourceBaseFolder%\*" (
echo %~nx0: There is no folder %SourceBaseFolder%
set "ErrorCount=1"
goto HaltOnError
)
cd /D "%SourceBaseFolder%"
if not exist "FileNames.txt" (
echo %~nx0: There is no file %SourceBaseFolder%\FileNames.txt
set "ErrorCount=1"
goto HaltOnError
)
set "ErrorCount=0"
for /F "usebackq delims=" %%N in ("FileNames.txt") do (
for /R %%J in ("%%N*") do (
set "FilePath=%%~dpJ"
if "!FilePath:%TargetBaseFolder%=!" == "!FilePath!" (
set "TargetPath=%TargetBaseFolder%\!FilePath:%SourceBaseFolder%\=!"
md "!TargetPath!" 2>nul
if exist "!TargetPath!\*" (
echo Copying file %%~fJ
copy /Y "%%~fJ" "!TargetPath!" >nul
) else (
set /A ErrorCount+=1
echo Failed to create directory !TargetPath!
)
)
)
)
:HaltOnError
if %ErrorCount% NEQ 0 (
echo.
pause
)
endlocal
Can any one fix this problem? Thanks in advance.
The solution you're trying to get to work seems excessively convoluted
I believe something similar to the following should do what you want
FOR /F %%G IN ('dir /a-d /s /b "D:\System backup\picture batch file\Test\15oct2015"^|findstr /I /E /G:"D:\System backup\picture batch file\Test\15oct2015\FileNames.txt"') DO (
copy "%%G" "C:\OutputFolder"
)
To ensure the filename matches are exact, all filenames in filenames.txt should be preceded by backslash, e.g.
\filename1.file
\filename2.file
You can easily generate such a file within a batch file, if necessary

creating folders based on Filename without overwriting

Working on a script whereby Staff Scans in Students Exams Certificates via Photocopier and it then places a copy to a location
the script below does the following
1) Scans to C:\users\location as filename.pdf (The Staff member will manually enter STudent number at the photocopier and it will save the file name as that)
2) Script then creates folder based on filename so here it will create a folder called /filename
3) Moves the PDF into that folder
it all works fine
however i am trying to modify it so it does not overwrite
so if for example staff member makes a typo of student1 and student1 already exists we dont want to override it
any suggestions
script is as below you can test it
Code:
#echo off
pushd C:\Users\location
for %%F in (*.pdf) do (
2>nul md "%%~nF"
>nul move /y "%%~nF*.*" "%%~nF"
)
popd
You cannot overwrite folder with MD so I suppose you are talking about move operation:
#echo off
pushd C:\Users\location
for %%F in (*.pdf) do (
2>nul md "%%~nF"
echo n|move /-y "%%~nF*.*" "%%~nF" >nul 2>nul
)
popd
or (might depend on local settings and the expected input by move /-y)
#echo off
pushd C:\Users\location
for %%F in (*.pdf) do (
2>nul md "%%~nF"
echo n|move /-y "%%~nF*.*" "%%~nF" 2>&1 |find /i "0 files(s)" >nul 2>nul || (
2>nul md "%%~nF_2"
move "%%~nF_2*.*" "%%~nF_2" >nul 2>nul
)
)
popd

Check for new folder after running bat commands

Is there a way to check if a new folder (exact name is unknown) has been created inside a specific parent folder as a result of running previous commands in bat file? If yes - run one more command with the full path to a new folder as an argument to that command.
Right now I am using a folder monitoring software to run another bat if new folder is created. I would like to have just one script performing both tasks.
Thank you for your help.
change the root_folder location on the second line
#echo off
set "root_folder=C:\something"
setlocal enableDelayedExpansion
set counter=1
for /d /r "%root_folder%" %%a in (*) do (
set "dirs[!counter!]=%%~sa" >nul
set /a counter=counter+1
)
rem ############################
rem # call your code here !!! #
rem ############################
call commands.bat
rem for /l %%l in (1,1,!counter!) do (
rem dir /x /b /s /a:d "%root_folder%" | findstr /i "dirs[%%l]"
rem )
set flag=0
for /d /r "%root_folder%" %%a in (*) do (
set dirs[|find /i "%%~sa" >nul 2>nul ||(
echo "%%~sa" is a new folder
set flag=1
)
)
if %flag% equ 0 (
echo no new folders
)

Batch file moves to location in argument

Thanks to rojo the batch file below will check for illegal characters, replace them with a dash, search subdirectories and require a location argument, but when it's run it takes you to the directory you specify in the argument. I want to stay in drive/folder the batch file is run from. (ex: rename.bat c:\test, this will take you to c:\test after batch file runs)
##echo off
IF "%~1"=="" goto Continue
pushd %1
setlocal enabledelayedexpansion
for /r %%I in (*) do (
set "file=%%~nxI"
if "!file:~0,1!"=="~" (
set "file=-!file:~1!"
)
for %%d in (# %%) do (
if not "!file!"=="!file:%%d=!" (
set "file=!file:%%d=-!"
)
)
if not "!file!"=="%%~nxI" (
echo %%~fI -^> !file!
ren "%%~fI" "!file!"
)
)
Exit /B
:Continue
#echo You need drive and directory at end or this batch file
At the end of your FOR /R loop, add POPD to "undo" the directory change performed by the respective PUSHD command.
setlocal enabledelayedexpansion
pushd %1
for /r %%I in (*) do (
...
)
REM Revert back to the original directory.
POPD
ENDLOCAL
Exit /B
:Continue

Batch-Scripting permission changes for a copied file and subroutine possibilities?

I am trying to achieve the following:
For every file in the source folder %1; Display it's details, ask the user whether the file should be copied and if the user answers "no" display a message "skipped", otherwise copy the file to the target folder %2, set the copy's permissions to "read-only", and display a message.
This is what I have managed so far but the last part I have had no luck understanding.
#echo off
rem if the source folder does not exist, display a message and exit
if exist "%1%" (
echo .
) else (
echo Source folder doesn't exist.
exit /b
)
if exist "%2%" (
echo Directory exists
) else (
md %2
echo Directory Created
)
FOR /F "DELIMS==" %%f in ('DIR "%1" /B') DO (
ECHO %%f
set p = NULL
SET /p p="Copy(y/n)?"
IF "%p%" == "y" (
COPY "%1\%%f" "%2"
ECHO Copied %%f to %2
) ELSE (
ECHO "%%f" Skipped
)
)
Is this possible? And if it is can it be done with a subroutine for the iteration process (copying and setting permissions)?
I made a batch script to do this operation
#echo off
::This batch file works with arguments or no
::copysec "sourcefolder" "destination folder"
if not "%1" == "" (
set "source=%1") else (
set /p "source=Source folder: ")
if not "%2" == "" (
set "destination=%2") else (
set /p "destination=Destination folder: ")
if not exist "%destination%" (
echo/Destination folder %source% not found&pause>nul&exit/b)
if exist "%source%" (
pushd "%source%") else (
echo/Source folder %source% not found&pause>nul&exit/b)
set /a count=0
for %%f in (*.*) do (
CALL:PROCESS "%%f")
echo/Finished. %count% files copied
popd&exit/b
:PROCESS file
cls
if "%~x1" == "" (exit/b)
echo/Full path: %~f1&echo/Disk: %~d1
echo/Name: %~n1&echo/Extension: %~x1
echo/Attributes: %~a1&echo/Time %~t1
echo/Size: %~z1 bytes&echo/
set /p "choice=Do you want to copy %1? [Y/N] "
if /i "%choice%" == "y" (
Goto Y)
echo/Skipped&timeout 2 1>nul&exit/b
:Y
copy "%~1" "%destination%\%~1" >nul || GOTO FAIL
attrib +R "%destination%\%~1" >nul || GOTO FAIL
:NEXT
set /a count=count+1
echo/Precess sucefull!&timeout 2 1>nul&exit/b
:FAIL
echo/Failed to copy %~1
pause>nul
exit/b
Well if you want to copy all files of all subfolders, you can use for /r
save it as copysec.bat.

Resources