Jump to content


Photo

Small 'programming' challenge


  • Please log in to reply
12 replies to this topic

#1 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 01 August 2012 - 12:37 PM

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:
  • 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".
  • the batch file has to be run from a Scheduled Task.
There are 2 aspects to the challenge:
  • 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 '/'.
  • 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:
  • the CMD file is running from the client's Win7 computer.
  • the Scheduled Task is using the client's login creds.
  • the client's ID is part of Administrators.
  • 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?

If at first you don't succeed, do it like your mother told you.


#2 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 01 August 2012 - 01:43 PM

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

HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#3 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 01 August 2012 - 01:58 PM

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?

If at first you don't succeed, do it like your mother told you.


#4 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 01 August 2012 - 11:20 PM

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, 01 August 2012 - 11:21 PM.

HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#5 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 02 August 2012 - 02:15 PM

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.

If at first you don't succeed, do it like your mother told you.


#6 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 02 August 2012 - 11:23 PM

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

HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#7 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 03 August 2012 - 08:18 AM

@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.

If at first you don't succeed, do it like your mother told you.


#8 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 04 August 2012 - 01:12 AM

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, 04 August 2012 - 01:19 AM.

HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#9 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 04 August 2012 - 12:53 PM

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:
  • CurrentDate.txt, RealDate.exe, and the CMD file are all in the same shared directory on the WHS2011 server;
  • 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.

If at first you don't succeed, do it like your mother told you.


#10 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 04 August 2012 - 02:21 PM

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, 04 August 2012 - 02:22 PM.

HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#11 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 04 August 2012 - 02:44 PM

I'm not familiar with pushd and popd. I'm very familiar with push and pop in assembly language, but not in this context. I will have to look it up.

If at first you don't succeed, do it like your mother told you.


#12 Mr_Smartepants

Mr_Smartepants

    HSS Pro

  • Members
  • 233 posts

Posted 05 August 2012 - 12:27 AM

Microsoft Windows [Version 6.1.7601]
Copyright © 2009 Microsoft Corporation. All rights reserved.

C:\>pushd /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.

PUSHD [path | ..]

path Specifies the directory to make the current directory.

If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.

C:\>popd /?
Changes to the directory stored by the PUSHD command.

POPD


If Command Extensions are enabled the POPD command will delete
any temporary drive letter created by PUSHD when you POPD that
drive off the pushed directory stack.


HP EX490 (E5200 CPU & 4GB RAM upgrades). DriverPacks.net Administrator and Team Lead. Some heroes don't wear capes, they wear Kevlar!
Stand-Alone Driver Disk (SAD2) update utility for DriverPacks.net (installs drivers automatically on all versions of Windows from 2000 to 2012).

#13 ikon

ikon

    HSS Genius

  • Donating Member
  • 8,575 posts

Posted 05 August 2012 - 01:57 PM

Yep. I found the same info. It's basically, and not surprisingly, given its name, the same concept as the assembly language push & pop. It just does paths instead of current memory locations.

I believe I now have a working script. I've tested it on my system at home and it seems to work fine. Just have to test it on my client's computers.

If at first you don't succeed, do it like your mother told you.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users