| 1. |
Solve : File manipulation (moving/renaming) in a batch file? |
|
Answer» Hello, You should get away with millisecond uniqueness unless you have a Cray supercomputer. Hmmmm, perhaps I'm doing something wrong then. The attached picture shows the destination directory (that will serve as the upload cache) after running the script. All files have the same number for the milliseconds portion of the time variable. Also, because of this, the files with the same name aren't copied over (i get an access is denied message when the script runs). I assume this is because a file with the same name already exists because those are the files that are left after going through the leftovers. Any idea what this could be? Thanks for your help. [file cleanup - saving space - attachment deleted by admin]Yeah, that was a glaring error. thetime will have to be recalculated each time through the loop. If you anticipate the script running through midnight, I guess thedate should be recalculated also. Code: [Select]@echo off for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (set thedate=%%l%%j%%k) for /f "tokens=* delims=" %%x in ('dir /a:d /s /b c:\images') do ( for /f "tokens=* delims=" %%i in ('dir /a:-d /s /b %%x') do ( for /f "tokens=1-4 delims=:." %%o in ("%time%") do (set thetime=%%o%%p%%q%%r) move "%%i" "c:\ftp_upload_cache\%%~ni_%thedate%_%thetime%%%~xi" ) ) REM ftp to another server I'll let you decide about the date token. What you said about calculating it each loop makes sense. Unfortunately, the script doesn't seem to calculate the time at all now. It puts the underscore in there before the time but the time is getting parsed or appended to the filename at all. Any ideas?Even when I get the time into the label, the milliseconds never seem to change. Code: [Select]@echo off setlocal enabledelayedexpansion for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (set thedate=%%l%%j%%k) for /f "tokens=* delims=" %%x in ('dir /a:d /s /b c:\images') do ( for /f "tokens=* delims=" %%i in ('dir /a:-d /s /b %%x') do ( for /f "tokens=1-4 delims=:." %%o in ("%time%") do ( set thetime=%%o%%p%%q%%r move "%%i" "c:\ftp_upload_cache\%%~ni_%thedate%_!thetime!%%~xi" ) ) ) REM ftp to another server However, in order to create unique file labels, why not just append a sequence number? A whole lot easier: Code: [Select]@echo off setlocal enabledelayedexpansion set seq=0 for /f "tokens=* delims=" %%x in ('dir /a:d /s /b c:\images') do ( for /f "tokens=* delims=" %%i in ('dir /a:-d /s /b %%x') do ( call set /a seq=%%seq%%+1 move "%%i" "c:\ftp_upload_cache\%%~ni_!seq!%%~xi" ) ) ) REM ftp to another server This code is a mess. Pray you never have to change it. The problem with the sequence number is that it would reset whenever the script was done and this could make for potential duplicate filenames. That's why I was hoping to make it date/time stamped with such a finite resolution. Is there a global count that could be instated somehow? Maybe I'm looking at this the wrong way. Were you able to get the time stamp to work? All machines are different, so I thought maybe on yours it would work. If you do decide to go with the sequence number you can persist the count in a file over multiple runs. Code: [Select]@echo off setlocal enabledelayedexpansion if exist count.dat (set /p seq=<count.dat) else (set seq=0) for /f "tokens=* delims=" %%x in ('dir /a:d /s /b c:\images') do ( for /f "tokens=* delims=" %%i in ('dir /a:-d /s /b %%x') do ( call set /a seq=%%seq%%+1 move "%%i" "c:\ftp_upload_cache\%%~ni_!seq!%%~xi" ) ) ) REM ftp to another server echo %seq% > count.dat I guess I don't understand why you can't ftp each directory separately where the file names were already unique Is this a good time to ask what HAPPENS to these files after the ftp? I was unable to get the timestamp to work. All the images go to one directory on a server that polls the directory for images/files (I was told it doesn't traverse the directory structure in its polling) to upload into the content management system of the hospital. Perhaps a persistent sequence is the way to go... |
|