Jump to content
RESET Forums (homeservershow.com)

Small 'programming' challenge


ikon
 Share

Recommended Posts

I have a small challenge at the moment. At first glance it seems doorknob simple, but I'm finding it a bit of a pest.

 

Here's the scenario. I want to rename a file from within a CMD file.......... OK, now that you've stopped laughing, let me elaborate.

 

There are a couple of wrinkles:

  1. as part of the rename, I want the date tacked onto the end of the filename, ahead of the extension: e.g. "This Is The File To Rename.txt" will become "This Is The File To Rename_2012-08-01.txt".
  2. the batch file has to be run from a Scheduled Task.

There are 2 aspects to the challenge:

  1. how to get the date in the correct format, without altering the System Date and Time settings. Since different people have different preferences for the date format, this can be tricky. Trying to use some formats will cause the batch file to fail; for example, if the date format includes slashes '/'.
  2. how to get the Rename command to run from a CMD file that's launched by a Scheduled Task.

I have some ways of getting around the date format issue, but the Rename command issue has me stumped.

 

To speed things up, let me cover a few of the simpler questions:

  1. the CMD file is running from the client's Win7 computer.
  2. the Scheduled Task is using the client's login creds.
  3. the client's ID is part of Administrators.
  4. the batch file works perfectly when run interactively from the client's session; it only fails when run as a Scheduled Task

My client is a relative that I'm helping out. His IT infrastructure is antiquated and I'm trying to bring him at least into the 21st century :)

 

Anyone got some magic smoke I can tap into?

Link to comment
Share on other sites

As long as the client's file permissions allow changes to the .txt file in question there should be no problem.

Here's a simple batch file to help you rename with date.

 


@ECHO off & setlocal EnableDelayedExpansion
SET "TargetFile=Document.zzz"


REM :: This sets the environment variable TimeStamp to the format YYYY-MM-DD.  Any leading zeros are dropped.
REM :: Adapted from: http://blogs.technet.com/b/pfe-ireland/archive/2008/05/08/batch-files-date-stamp-in-a-filename.aspx
SET "TimeStamp="
FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do SET "datestr=%%l-%%j-%%k"

SET "TimeStamp=%datestr%"
REM :: Strips any spaces from the variable and replaces them with underscores.
SET "TimeStamp=%TimeStamp: =_%"

FOR %%A in (%TargetFile%) do (SET "NewFile=%%~nA")
MOVE "%TargetFile%" "%NewFile%_%timestamp%.txt"

endlocal

:eof

Link to comment
Share on other sites

Well, as I said, the the batch file works perfectly if it's run interactively. It's only from a Scheduled Task that there's a problem. I have a For statement in the CMD file, but it just doesn't work when scheduled.

 

Something I forgot to mention in my 1st post is that the Task gives me a 0x2 return code. Now, normally, my understanding is that this means 'file not found', but that makes little to no sense in this context, because the directory where the file resides is accessed just fine from the rest of the CMD commands.

 

BTW, looking at your batch file, isn't there a potential problem with

 

FOR /f "tokens=1-4 delims=/ " %%i in ("%date%") do SET "datestr=%%l-%%j-%%k"

 

What happens if the client has set the date format to something that, for example, doesn't use '/'s as delimiters?

Link to comment
Share on other sites

What happens if the client has set the date format to something that, for example, doesn't use '/'s as delimiters?

Actually, the code I used uses both '/' and ' ' (space) as delimiters which should catch 90% of the date formats. You can change it to match the unique date format in use in your area. The easy way to check is to open a command prompt and type "echo %date%" (no quotes) and see what the output contains.

Then change the delims= to a dash, or whatever is appropriate.

 

As for the scheduler problem, I don't know.

Edited by Mr_Smartepants
Link to comment
Share on other sites

Thanks for responding. Appreciate it. I've actually solved the date problem. I wrote a small WinBatch program that queries the system for the raw date, then formats it to YYYY-MM-DD and writes it out to a text file. The CMD file then reads that text file using a FOR /F command. However, I still can't get around the issue of the scheduled version of the CMD not being able to find the file. I've even tried mapping a drive, but no go.

Link to comment
Share on other sites

If the file is in use, it will have a tilde '~' in it's file name. If you're hard-coding the filename in your script, it will fail.

Can you post the content of your script so we can help debug it?

 

If the target file or destination is a UNC network path, you may need to use pushd and popd to assign a temp drive letter.

 

:path-check
:: Check for network paths, assign temporary drive letter if found.
SET pd=%~dp0
pushd %~dp0
SET cur=%cd%

:: Release temporary drive letter for network paths, if found.
popd & endlocal & EXIT

Link to comment
Share on other sites

@call "\\server\share\RealDate.exe"

For /F %%A IN (CurrentDate.txt) do rename "\\server\share\testfile.txt" "testfile_%%A.txt"

RealDate.exe is a routine I wrote that gets the date in the Windows built-in native format (so it's always the same, no matter what format the user chooses for display purposes) converts it to YYYY-MM-DD format, and writes it out to CurrentDate.txt.

 

What fails is the "For /F" line. It claims it cannot find the file. It doesn't specify which file, but RealDate.exe, CurrentDate.txt, and the CMD file are all in \\Server\share.

 

The Scheduled Task has its Start In field set to \\Server\share.

 

There is only 1 line in CurrentDate.txt.

Link to comment
Share on other sites

You need to specify the path to currentdate.txt unless it exists in the same path as the batch file that is running.

 

 

SET "filepath=\\server\share"
@call "%filepath%\RealDate.exe"
For /F %%A IN (%filepath%\CurrentDate.txt) do rename "%filepath%\mytestfile.txt" "testfile_%%A.txt"

Edited by Mr_Smartepants
Link to comment
Share on other sites

Wow, I could have sworn I covered that, and something else, in my original post but, on reviewing it just now, I don't see it. I think I must have typed it up and forgot to submit it.

 

Anyway, 2 things:

  1. CurrentDate.txt, RealDate.exe, and the CMD file are all in the same shared directory on the WHS2011 server;
  2. The Start In item in the Scheduled Task is set to that same shared directory.

I know for sure that RealDate.exe is executing OK because it does create CurrentDate.txt correctly.

 

I also discovered that you apparently cannot use a path for the IN part of the FOR commend. I tried

 

For /F %%A IN (\\server\share\CurrentDate.txt)

 

and it bombed immediately. It seems that the IN parameter can't contain a path. I don't know this for sure, but it worked when I removed the path and bombed with it added it. I even tried a mapped drive; it bombed too.

Link to comment
Share on other sites

That's true, I forgot, network UNC paths can't be used. You need to use a full path with drive letter IIRC. That's why I suggested pushd and popd earlier. Makes life simple.

Mapping the drive is still translated to UNC by the system.

Edited by Mr_Smartepants
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...