Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: File manipulation (moving/renaming) in a batch file  (Read 6625 times)

0 Members and 1 Guest are viewing this topic.

marfpilf

  • Guest
File manipulation (moving/renaming) in a batch file
« on: December 28, 2007, 02:30:59 PM »
Hello,

I am writing a simple batch file to run on an XP SP2 environment.
What I'm trying to do is:

Go through a folder's subfolders and rename the files inside the various folders to a unique name, then move all the files to one folder to be ftp'd off to another server.

The problem is, the program that creates these files/folders has a structure like this:
<root folder>
     UNI00001
          uni00001.tif
          uni00001.cfg
          uni00001.csv
     UNI00002
          uni00001.tif
          uni00002.tif
          uni00003.tif
          uni00002.cfg
          uni00002.csv
     UNI00003
          uni00001.tif
          uni00002.tif
          uni00003.tif
          uni00003.cfg
          uni00003.csv


I'm sort of spinning my wheels when it comes to getting this thing down to the syntax.  The pseudo-code I have written out based on my research goes something like:

traverse the directory structure (for loop?)
append a date/time stamp before the extension of all the files (I looked over the guide on this and might have an idea)
move all renamed files to some directory X (the move/copy command)
ftp the files to another server (via the ftp program, still working out the details there)


I mostly need help with framing up loops to run the various commands for renaming and moving.  I have searched the forums and read some threads but couldn't really find anything.  Thanks in advance for any replies and I hope I gave you enough information.

Steven

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: File manipulation (moving/renaming) in a batch file
« Reply #1 on: December 28, 2007, 03:04:54 PM »
This little snippet should traverse all the directories one at a time. You probably should clear out directoryX after each ftp session but that's your call.


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:\rootdir') do (
for /f "tokens=* delims=" %%i in ('dir /a:-d /s /b %%x') do (
move "%%i" "c:\directoryX\%%~ni%thedate%%%~xi"
)
ftp to another server
)

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

marfpilf

  • Guest
Re: File manipulation (moving/renaming) in a batch file
« Reply #2 on: January 04, 2008, 02:25:22 PM »
Thanks a ton Sidewinder, your script worked perfectly for traversing the directory and renaming/moving the files.  However, I have run into another roadblock.  I was hoping the timestamp down to the millisecond would take care of the uniqueness problem that some of the files have when moved all into one directory.  What do you recommend I do to keep these files uniquely named?  Here is the script for reference:

Code: [Select]
@echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (set thedate=%%l%%j%%k)
for /f "tokens=1-5 delims=:" %%o in ("%time%") do (set thetime=%%o%%p%%q)

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 (
move "%%i" "c:\ftp_upload_cache\%%~ni_%thedate%_%thetime%%%~xi"
)
REM ftp to another server
)

Thanks again for all of your help.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: File manipulation (moving/renaming) in a batch file
« Reply #3 on: January 04, 2008, 03:18:33 PM »
The original code was setup to gather all the files in the first directory (UNI00001), append the date, send them to directoryX and ftp them off to dataland. This cycle would be repeated for each directory. That's why I suggested you empty out directoryX, so it would be empty for the next set of files. I saw no need for uniqueness (obviously appending the date wouldn't do it)

You should get away with millisecond uniqueness unless you have a Cray supercomputer. ;D

Code: [Select]
@echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (set thedate=%%l%%j%%k)
for /f "tokens=1-4 delims=:." %%o in ("%time%") do (set thetime=%%o%%p%%q%%r)

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 (
move "%%i" "c:\ftp_upload_cache\%%~ni_%thedate%_%thetime%%%~xi"
)
)
REM ftp to another server

If you're going to ftp all the files in one shot, move the ftp command outside the loop (as seen above).
« Last Edit: January 04, 2008, 03:41:18 PM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

marfpilf

  • Guest
Re: File manipulation (moving/renaming) in a batch file
« Reply #4 on: January 08, 2008, 12:13:05 PM »
You should get away with millisecond uniqueness unless you have a Cray supercomputer. ;D

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]

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: File manipulation (moving/renaming) in a batch file
« Reply #5 on: January 08, 2008, 01:02:14 PM »
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.  8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

marfpilf

  • Guest
Re: File manipulation (moving/renaming) in a batch file
« Reply #6 on: January 08, 2008, 02:29:48 PM »
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?

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: File manipulation (moving/renaming) in a batch file
« Reply #7 on: January 08, 2008, 04:00:09 PM »
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. ;D

The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

marfpilf

  • Guest
Re: File manipulation (moving/renaming) in a batch file
« Reply #8 on: January 09, 2008, 10:21:14 PM »
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. 

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: File manipulation (moving/renaming) in a batch file
« Reply #9 on: January 10, 2008, 04:51:04 AM »
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?

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

marfpilf

  • Guest
Re: File manipulation (moving/renaming) in a batch file
« Reply #10 on: January 10, 2008, 01:03:49 PM »
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...