Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cmd Puzzler 4

Status
Not open for further replies.

harebrain

MIS
Feb 27, 2003
1,146
US
This cmd script is giving me a headache. (I inherited it from I don't know whom, and was told to make it work.)

It's a simple version-saver: invoked in Explorer's context menu, it copies the selected file to a folder named "PreviousVersions." It inserts a date/timestamp into the filename.

The weird thing is that on our W2k server it works fine. But on the XP servers, it creates the file with a short filename, i.e., %FN% becomes something like XXXXXX~9. Obviously, we want the long filename.

And, oh yeah, I've tried using "for" to parse for the dot and break things up myself, but my new friends think it's a great idea to use periods as often as they please in a filename.

Any ideas?

Code:
@echo off

cd /d %~dp1

if exist "%~dp1\PreviousVersions" goto :COPYFILE
md "%~p1\PreviousVersions"

:COPYFILE

set FN=%~n1
set XN=%~x1

for /f "tokens=1-6 delims=/: " %%a in ("%~t1") do (
set DT=%%c%%a%%b
set Hours=%%d
set Minutes=%%e
set AMPM=%%f)

set DT=%DT:~0,8%
set Minutes=%Minutes:~0,2%
set Hours=%Hours:~0,2%
if %Hours% LSS 12 if /I "%AMPM%"=="PM" set /A Hours=%Hours%+12
if %Hours% EQU 12 if /I "%AMPM%"=="AM" set Hours=00

copy "%1" ".\PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"
attrib +R ".\PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"

:END
 
I don't know scripts but what about something like this?

Open up the registry editor and go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem. Create a new DWORD value named NtfsDisable8dot3NameCreation. Set it to 1.

How to Disable the 8.3 Name Creation on NTFS Partitions
 
try

Code:
@ECHO OFF

FOR /F "usebackq tokens=1*" %%J IN (`DIR %~s1 /b`) DO CALL :DOIT %%J "%%K" %~sdp1

GOTO :EOF

:DOIT
SETLOCAL
IF '%2'=='""' SET LFN=%~1
IF NOT '%2'=='""' SET LFN=%~1 %~2
CALL :ORIG "%3%LFN%"
ENDLOCAL
GOTO :EOF

:ORIG
pushd %~dp1

if NOT exist "%~dp1\PreviousVersions" md "%~p1\PreviousVersions"

set FN=%~n1
set XN=%~x1

for /f "tokens=1-6 delims=/: " %%a in ("%~t1") do (
set DT=%%c%%a%%b
set Hours=%%d
set Minutes=%%e
set AMPM=%%f)

set DT=%DT:~0,8%
set Minutes=%Minutes:~0,2%
set Hours=%Hours:~0,2%
if %Hours% LSS 12 if /I "%AMPM%"=="PM" set /A Hours=%Hours%+12
if %Hours% EQU 12 if /I "%AMPM%"=="AM" set Hours=00


copy "%~1" ".\PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"
attrib +R ".\PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"
popd

GOTO :EOF
 
Justin,

Some of your code was cut off. Please re-post your program.

Regards,
David.
 
Thanks, Justin! That really does the trick.

I had tried using:

for /f "tokens=1-2 delims=." %%g in ('dir /b ^"%1^"') do

so that was along the line I was thinking, but I just couldn't get over the hump of capturing the entire filename.

And thanks, Linney, for the ideas. I'd bet real money, though, that our IT dept wouldn't let me mess with the registry settings on their servers. :)
 
If anyone is following along, I modified the script to rectify a few subtle bugs (which were in the original script.)
Code:
@ECHO OFF

FOR /F "usebackq tokens=1*" %%J IN (`DIR %~s1 /b`) DO CALL :DOIT %%J "%%K" %~sdp1
GOTO :EOF

:DOIT
SETLOCAL
IF '%2'=='""' SET LFN=%~1
IF NOT '%2'=='""' SET LFN=%~1 %~2
CALL :ORIG "%3%LFN%"
ENDLOCAL
GOTO :EOF

:ORIG
pushd %~dp1

if NOT exist "%~dp1PreviousVersions" md "%~dp1PreviousVersions"

set FN=%~n1
set XN=%~x1

for /f "tokens=1-6 delims=/: " %%a in ("%~t1") do (
set DT=%%c%%a%%b
set Hours=%%d
set Minutes=%%e
set AMPM=%%f)

set DT=%DT:~0,8%
set Minutes=%Minutes:~0,2%
set Hours=%Hours:~0,2%
if /I "%AMPM%"=="PM" if %Hours%==08 set Hours=8
if /I "%AMPM%"=="PM" if %Hours%==09 set Hours=9
if /I "%AMPM%"=="PM" set /A Hours=%Hours%+12
if %Hours% EQU 12 if /I "%AMPM%"=="AM" set Hours=00

copy "%~1" "%~dp1PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"
attrib +R "%~dp1PreviousVersions\%FN%_%DT%_%Hours%%Minutes%%XN%"
popd

GOTO :EOF
I hardwired the drive letter everywhere because of the way our servers are setup. This negates the effects of PUSHD/POPD, but YMMV.

Note the special tests for 08:nn PM and 09:nn PM. Why? Numbers with a leading zero are treated as octal! For 00 to 07, no problems: they're converted to decimal and added to 12 for a correct result. 08 and 09, though, throw an error when the addition is attempted: the result is that they remain 08 and 09 and end up in the file name looking like they are AM times.
 
The OP says "XP servers" and further down mentions about the IT department not wanting him to play with the registry.

Erm XP is a client not a server. Implement a server then you don't need to faff about with backup scripts, you can use previous versions to achieve exactly this automatically and a lot easier for users to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top