1

We have a directory that gets loaded up with 40+ files that have different file names. They all start out with the same file name structure, but a date is appended to the end.

Example:

FILE.txt.01012013
FILE.txt.01022013
FILE.txt.01032013

I need to create a batch file that will do a few complex and some not so complex things:

  1. select only one file.
  2. Rename that file to another name. (Example: TEST.txt) Note: The name of the renamed file will always be TEST.txt)
  3. Move the renamed file from it's current directory to a new directory.
  4. 15 minutes later...start off with step 1 again. (Note: This needs to continue to run until there are no more files left in the original directory.)

What I have tried: My skill level on batch files is very basic, and therefore I have been trying to search the web for suggestions. I can find code to rename a file (but you have to state the original file name). I can find code to find a file using a * in the filename, but I would think that selects all the files in the directory. I need it to happen to one file at a time, and every 15 minutes. Once a file is renamed and moved to the new directory... There is a file watcher process that takes that file (in this example TEST.txt) and ingests the data. Once the data is ingested the file is deleted. This means that when the next file is renamed to TEST.txt and moved to the directory there will not be any reason to overwrite the previous file.

2
  • 1
    Welcome to superuser. Can you tell us what you've already tried? By editing your post to make it a question, you improve your chance of getting a detailed answer. Also post the code you've already started and add any details, like what OS this will be used with, would be very helpful helpful.
    – CharlieRB
    Commented Apr 8, 2013 at 18:45
  • I have added more detail, however unfortunatly I am not thinking it will help very much.
    – JP Hooks
    Commented Apr 8, 2013 at 20:18

3 Answers 3

0

This seems like what you want, however you need to fill in the gaps.

set count=0
for /f %%i in (file_path\*.*) do set /a count+=1
set test=%count%
:loop
if %test% neq %count% timeout /nobreak 900 >nul
set /a num=%random% %% %count% + 1
set /a count-=1
if %count% leq 0 goto end
for /f "skip=%num%" %%i in (file_path\*.*) do (
    ren %%i TEST.txt
    move TEST.txt file_path\
    goto loop
)
:end

What it does:

  1. Find out how many files in a folder (that you need to change).
  2. Make a random number with the max being the amount of files in the folder.
  3. Take one off the max number, for next time.
  4. Skip a random amount of files (designated by the random number) and choose the next one.
  5. Rename the file to TEXT.txt and move it to file_path\ (which you need to change).
  6. Wait for 15 minutes (900 seconds).

Hope that helps.

Note, you need to change **file_path** to the appropriate files.

6
  • This will not work, for /f doesn't read directories so. Use dir command or a simple for loop without qualifier.
    – Endoro
    Commented Apr 9, 2013 at 17:06
  • What does "skip" do in for /f "skip=%num%" %%i in (... ? Commented Apr 10, 2013 at 0:17
  • @KevinFegan It was meant to skip a certain amount of files and choose the next one. This was basically it choosing a random file.
    – BDM
    Commented Apr 10, 2013 at 0:34
  • @Endoro The OP said that a random file was to be chosen. Nonetheless, you're correct.
    – BDM
    Commented Apr 10, 2013 at 0:35
  • I think when the OP said Randomly they were talking about finding a file where the full name is not known ahead of time (something like Randomly named). I don't think they were talking about the order in which files are processed. Hopefully the OP will clarify this. Commented Apr 10, 2013 at 2:12
0

Try this, it does all the things you asked for:

@echo off&setlocal enabledelayedexpansion
set "startfolder=folder1"
set "targetfolder=folder2"

cd /d "%startfolder%"
:randomstart
set /a filecnt=0
for %%i in (*) do set /a filecnt+=1
if %filecnt% equ 0 echo(no file found in %Startfolder%&goto:eof
set /a rd=(%random%%%%filecnt%)+1
set /a cnt=0
for %%i in (*) do set /a cnt+=1&if "!cnt!" equ "%rd%" set "randomfile=%%~i"
echo "%randomfile%"
move "%randomfile%" "%targetfolder%\test.txt"
ping -n 900 localhost >nul
goto:randomstart
-1

Here is a simple batch script to process the files:

@echo off

rem     set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"

rem     set filename for renamed files
set "zzfinalname=TEST.txt"

rem     set source folder here
set "zzsourcepath=C:\source\"

rem     set destination folder here
set "zzdestpath=C:\dest\"

rem     set your loop delay here
set "zzdelayminutes=15"

rem     **********************************
rem     might be good to add some error checking here
rem     **********************************


:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60


:restart
rem:restart

for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work "%%~f"

timeout /t %zzdelayseconds%
goto :restart


:work
rem:work

set "zz_w1=%~1"
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1

echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1

timeout /t %zzdelayseconds%

rem     Go get next file, if any.
goto :EOF



If you want a more complete solution, the batch script below includes:

  • Fairly complete validation (error checking) of input values.
  • With simple modification, values could safely be entered from command line.
  • At start, checks if existing target file is present and waits for external process to remove file.
  • Checks and reports on failed rename and move operations.
  • Resume after quit/fail processes files renamed but not moved.
  • Allows (optional) prompted delay (Choice.exe) allowing "Continue now" and graceful "Quit".


@echo off

rem     set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"

rem     set filename for renamed files
set "zzfinalname=TEST.txt"

rem     set source folder here
set "zzsourcepath=C:\source"

rem     set destination folder here
set "zzdestpath=C:\dest"

rem     set your loop delay here
set "zzdelayminutes=15"

rem     select  Prompted-delay, or simple Timeout-delay. P=Prompted, otherwise simple Timeout
rem     For Prompted-delay "Choice.exe" must be present on the computer.
set "zzdelaytype=T"


rem     **********************************
rem     error checking
rem     **********************************


:checksourcepath
rem:checksourcepath

rem     insure source path is valid (exists), and has trailing slash
if "%zzsourcepath%."=="." goto :sourcepatherror
set zzt1=%zzsourcepath:~-1,1%
if not "%zzt1%."=="\." set "zzsourcepath=%zzsourcepath%\"
if not exist "%zzsourcepath%*.*" goto :sourcepatherror
goto :checkdestpath


:sourcepatherror
rem:sourcepatherror

echo.
echo Error processing source path: [%zzsourcepath%].
echo.
call :cleanexit
exit /b 1


:checkdestpath
rem:checkdestpath

rem     insure dest path is valid (exists), and has trailing slash
if "%zzdestpath%."=="." goto :destpatherror
set zzt1=%zzdestpath:~-1,1%
if not "%zzt1%."=="\." set "zzdestpath=%zzdestpath%\"
if not exist "%zzdestpath%*.*" goto :createdestpath
goto :checkname


:createdestpath
rem:createdestpath

md "%zzdestpath%" >nul 2>&1
if not exist "%zzdestpath%*.*" goto :destpatherror
goto :checkname


:destpatherror
rem:destpatherror

echo.
echo Error processing destination path: [%zzdestpath%].
echo.
call :cleanexit
exit /b 2


:checkname
rem:checkname

if "%zzfinalname%."=="." goto :nameerror
goto :checkdelayminutes


:nameerror
rem:nameerror

echo.
echo Error: Rename target filename cannot be empty.
echo.
call :cleanexit
exit /b 3


:checkdelayminutes
rem:checkdelayminutes

set zzt1=0
set zzt2=1
set /a zzt1=zzt2*zzdelayminutes
if "%zzt1%."=="." goto :minute1serror
if %zzt1% LEQ 0 goto :minutes1error
if %zzt1% GEQ 1441 goto :minutes2error
goto :checkpattern


:minutes1error
rem:minutes1error

echo.
echo Error: Minutes must be a number greater than 0.
echo.
call :cleanexit
exit /b 4


:minutes2error
rem:minutes2error

echo.
echo Error: Minutes must be a number not greater than 1440 (1 day).
echo.
call :cleanexit
exit /b 5


:checkpattern
rem:checkpattern

rem     pattern must have at least 1 "*"
if "%zzmatchpattern%."=="." goto :patternerror
echo "%zzmatchpattern%"|find "*">nul
if %errorlevel% EQU 0 goto :start
rem goto :patternerror


:patternerror
rem:patternerror

echo.
echo Error: Pattern cannot be empty, and must contain at least 1 "*".
echo.
call :cleanexit
exit /b 6


:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60
set /a zzpartialdelay=zzdelayseconds/3
set zzexiting=0


:restart
rem:restart

rem     if destination file already exists, wait a bit more

if not exist "%zzdestpath%%zzfinalname%" goto:checkaborted

call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart


:checkaborted
rem:checkaborted

if not exist "%zzsourcepath%%zzfinalname%" goto :work1
echo.
echo Completing previously started file move.
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
goto :restart


:work1
rem:work1

set zzdelayflag=1
set zzsuccess=0
for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work2 "%%~f"

if %zzexiting% NEQ 0 exit /b %zzexiting%

echo %zzsuccess% file(s) processed.

if %zzdelayflag% EQU 0 goto :checksuccess
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart


:checksuccess
rem:checksuccess

if %zzsuccess% NEQ 0 goto :restart

echo.
echo Failed to rename all existing files, exiting.
call :cleanexit
set zzexiting=7
exit /b %zzexiting%
goto :EOF


:work2
rem:work2

if %zzexiting% NEQ 0 exit /b %zzexiting%

set "zz_w1=%~1"
set zzdelayflag=0
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1
if exist "%zz_w1%" goto :renameerror
if not exist "%zzsourcepath%%zzfinalname%" goto :renameerror
goto :movefinalfile


:renameerror
rem:renameerror

echo Error: Failed to rename file "%zz_w1%" to "%zzfinalname%"
echo.

rem     Rename failed, skip it and get next file (immediately), if any.
goto :EOF


:movefinalfile
rem:movefinalfile

rem Rename success, move the file

if not exist "%zzdestpath%%zzfinalname%" goto :domove

call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :movefinalfile


:domove
rem:domove

echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
if exist "%zzsourcepath%%zzfinalname%" goto :moveerror
goto :movecomplete


:moveerror
rem:moveerror

echo Error: Failed to move file "%zz_w1%" to "%zzdestpath%%zzfinalname%"

rem     Move failed, restore (un-rename) file and skip it and get next file (immediately), if any.

for %%g in ("%zz_w1%") do set "zzt1=%%~nxg"
echo Restore file: "%zzsourcepath%%zzfinalname%" to "%zzt1%"
ren "%zzsourcepath%%zzfinalname%" "%zzt1%">nul 2>&1
echo.

goto :EOF


:movecomplete
rem:movecomplete

set /a zzsuccess+=1
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
rem echo.

rem     Go get next file, if any.
goto :EOF


:dodelay
rem:dodelay

set zztime=%1

if /I "%zzdelaytype%."=="P." goto :dopauseddelay

echo.
timeout /t %zztime%
goto :EOF


:dopauseddelay
rem:dopauseddelay

echo.
echo Waiting (%zztime% seconds) to process next file... You can wait to 
echo continue after delay or Q(uit) or C(ontinue) now.
choice /c cq /n /t %zztime% /d c /m "Press Q(uit) or C(ontinue now) or No Action (wait) to continue after delay. "
if %errorlevel% EQU 1 goto :EOF

echo.
echo User selected Q(uit), exiting.
call :cleanexit
set zzexiting=254
exit /b %zzexiting%
goto :EOF


:waitexternal
rem:waitexternal

echo.
echo Waiting for externl process...
call :dodelay %zzpartialdelay%
if %zzexiting% NEQ 0 exit /b %zzexiting%

goto :EOF


:cleanexit
rem:cleanexit

set "zzdelayflag="
set "zzdelayminutes="
set "zzdelayseconds="
set "zzdelaytype="
set "zzdestpath="
set "zzexiting="
set "zzfinalname="
set "zzmatchpattern="
set "zzpartialdelay="
set "zzsourcepath="
set "zzsuccess="
set "zzt1="
set "zzt2="
set "zztime="
set "zz_w1="

goto :EOF

If you need any of this modified or explained, just let me know.

2
  • 1
    I wonder what the downvote was for...?
    – BDM
    Commented Apr 10, 2013 at 10:11
  • @ProfPickle - +1 for the perfect comment - thanks, I was wondering that myself. If I knew, maybe I could fix it... but as it stands now, I don't have a clue. Oh well, some ups, some downs... it all averages out. Commented Apr 11, 2013 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .