Batch from a - b, b to a via c, append time

January 13, 2011 at 01:44:14
Specs: Win server 2k3
Great News Everyone! As the great Professor Hubert J. Farnsworth once said it.

Great forum! I’ve been spending days and days in this forum before finally cavin’ in and registering.
I am finding batch challenging at the moment…
I have 3 locations, location A, B and TEMP

Location A generates one file called something.soc
This file needs to be moved to location B. What I have done is written a small script that maps up a network path, copies and moves as prescribed.

Location B gets the file, does it’s magic with it.

Location B then gives birth to two files, of unknown filetype and name. This happens at random times. These 2 files goes in 2 separate directories.

Location B’s assignment is then to send it to a location B subdirectory via the TEMP directory.

BUT in Location A’s directory there always have to be one file left behind. One file can never be moved.

Before the files enter Location B, they have to be renamed. E.G the creation date and time has to be appended to the filename so I can be absolutely certain no harm can be done to the other files that are there.

So far so good.

What I have done so far. Location A sends a file to Location B.

Any good advice on the last bits? I just can't make it work :-(

Thanks in advance.

See More: Batch from a - b, b to a via c, append time

January 13, 2011 at 02:38:52
Can you post your scripts, so we can have a look.

Happy is ONE who says I am an OttoMAN.

Report •

January 13, 2011 at 03:23:55
@echo off
set srcDir= "H:\groups\rem
set frcDir= "H:\groups\fin
set lastmod =

pushd %srcDir%

(For loop to check for lastmodified file)
if "%lastmod%"=="" echo could not locate files.&goto :eof

:I found a file, now lets copy and move
net use Y: "\\servername\groups\LocB\Import" /PERSISTENT :NO
copy %lastmod% Y:

:File is copied, and stores in a finished exporting directory
move %lastmod% %frcDir%


net use Y: /DELETE /Y

Report •

January 13, 2011 at 06:11:17
Run this in A and post output:

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
echo.%%~Ta %%a

Life is too important to be taken seriously.


Report •

Related Solutions

January 13, 2011 at 06:32:54

The file moving from location A works fine, and is no problem whatsoever.

from B to C is the one I cannot comprehend. I can't identify by filename, and have to append timestamp (day and time of creation) before moving it.

So I think a revised question would be:

How can I append X number of files in 2 different directories with date and time-stamp. The files does not have a file ending.


Report •

January 13, 2011 at 07:06:37
Depending on nature of what you're doing, you might get away with using the archive file attribute, and end up with something like this:
xcopy /m /i "H:\groups\rem\*" "H:\groups\fin"

How To Ask Questions The Smart Way

Report •

January 13, 2011 at 23:39:17
Still need to append date/time to the end of the filename though.

Xcopy just copies and takes the attribs over to the dark side.

Thanks anyway!

Report •

January 19, 2011 at 01:09:35
OK, girls and boys. I have officially given up. Not yet though.

This is what I have for my script.

Part one iterates through directory a, and copies the last modified file to the temp directory.

Part two iterates through directory b, and copies the last modified file to the temp directory.

Part three iterates through the temp directory, appending time/date to the end of the filename.

Any good pointers on how to accomplish the last part, because I am as stuck as a stuckup pick in a den full of sticks.

This is my code sofar:

SetLocal EnableDelayedExpansion
set kvitt1="c:\a"
set kvitt2="c:\b"
set tmpDir="c:\temp"

REM lets go to this directory, and scan for files, and copy to the temp
pushd %kvitt1%

for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :TWO

COPY "%Lastmod%" %tmpDir%

@ping -n 2 -w 1000 > nul
@ping -n %1% -w 1000> nul

@ping -n 2 -w 1000 > nul
@ping -n %1% -w 1000> nul

REM Let's go to the next directory, and scan for files to copy to the temp
REM Gå til ny nettverksstasjon
pushd %kvitt2%

for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :EOF

COPY "%LASTMOD%" %tmpDir%
@ping -n 2 -w 1000 > nul
@ping -n %1% -w 1000> nul


REM we have copied the files we need, lets skip to the temp directory for renaming and filenaming

pushd %tmpDir%
echo %tmpDir%

dir /b "%tmpDir%" >>filelist.txt
REM iterates through all lines in filelist.txt. %%i contains the filename
for /f %%i in (filelist.txt) do
ren "%%T" %!random%"%%~nT.kvi")

delete contents of filelist.txt (or delete filelist?)

AND THIS CODE actually works, though it doesnt give me time, only date.

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1-3,* skip=4" %%a in ('dir *.* /tc /a-d ^| find /v "(s)"') do (
set CDate=%%a
set dd=!CDate:~0,2!
set mm=!CDate:~3,2!
ren %%d %%~nd!dd!!mm!%%~xd


Report •

January 20, 2011 at 06:55:22
Nobody? Bumping a post is of course allowed, highly recommended, and politely accepted, isn't it?

Report •

January 20, 2011 at 08:20:41
  set CDate=%%~Ta
  set Month=!CDate:~0,2!
  set Day=!CDate:~3,2!
  set Year=!CDate:~6,4!
  set Hour=!CDate:~11,2!
  set Minute=!CDate:~14,2!

Report •

January 21, 2011 at 01:41:16
Thanks for the reply!

Is my script a good effort, and how can I improve my script?

Any ideas or suggestions?

Report •

January 21, 2011 at 02:11:51
What is wrong with this code for iterating through a directory, and renaming the files in it with time and date?

FOR %%T in (%tmpDir%*.*) DO RENAME "%%T"

Report •

January 21, 2011 at 07:23:39
Personally I would pars the date at the top so you can use the variables through out your code. Just add...

Set MM=%date:~4,2%
Set DD=%date:~7,2%
Set YYYY=%date:~-4%

to you variable initialization and then you could just do...

FOR %%T in (%tmpDir%*.*) DO (
RENAME "%%T" "%%T%YYYY%%MM%%DD%"

Personally, I would not rename the files but put the files in folders. Makes them easier to handle. Here is what I would do...

FOR %%T in ('dir %tmpDir%*.* /b') DO (
MD %tmpDir%\%YYYY%%MM%%DD%
COPY "%%T" "%tmpDir%\%YYYY%%MM%%DD%"

Report •

Ask Question