Do you speak Batch?

Any other tech-related topics
Message
Author
User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: Do you speak Batch?

#16 Post by guinness »

RecyCom V1.1.0.0

This is just a bug fix release.

User avatar
Midas
Posts: 6727
Joined: Mon Dec 07, 2009 7:09 am
Location: Sol3

Re: Do you speak Batch?

#17 Post by Midas »

EDIT: For Windows XP and up (provided WMIC is available), a much leaner script than the one I posted further down (benefiting from insight gathered from http://stackoverflow.com/questions/5594121/):

Code: Select all

ECHO.
ECHO USE: SWAP.BAT PARAM#1 PARAM#2 (USE TAB FOR AUTO COMPLETION IF AVAILABLE...)
ECHO.
ECHO REALLY SWAP "%1" NAME WITH "%2"?
ECHO.
ECHO (CTRL+C TO ABORT OR ANY KEY TO PROCEED...)
PAUSE

SETLOCAL

REM RECORD ORIGINAL FILESYSTEM OBJECTS NAMES

SET fsObjA=%1
SET fsObjB=%2

REM GENERATE A CLEAN DATE-TIME STRING IN "%%DTuniq%%"

FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTuniq=%%a
SET DTuniq=%DTuniq:~0,8%-%DTuniq:~8,6%

REM DO THE NAME SWAP, CREATE LOG AND END

MOVE %1 %DTuniq% > nul
MOVE %2 %fsObjA% > nul
MOVE %DTuniq% %fsObjB% > nul
ECHO %DTuniq%: %fsObjA% name swapped with %fsObjB% > swap-%DTuniq%.log
ECHO.
ECHO DONE!
== ORIGINAL POST FOLLOWS...

Unrelated to the RecyCom update, but this thread picked my fancy about my ability to come up with an independent solution to the prior file/folder swap challenge. Here's what I could come up with...

Code: Select all

ECHO.
ECHO USE: SWAP.BAT PARAM#1 PARAM#2 (USE TAB FOR AUTO COMPLETION IF AVAILABLE...)
ECHO.
ECHO REALLY SWAP "%1" NAME WITH "%2"? 
ECHO.
ECHO (CTRL+C TO ABORT OR ANY KEY TO PROCEED...)
PAUSE

SETLOCAL

REM RECORD ORIGINAL FILESYSTEM OBJECTS NAMES

SET fsObjA=%1
SET fsObjB=%2

REM GENERATE A CLEAN TIME_STRING FOR "%%tsclean%%"

SET "tsclean=%time:~0,8%" &:: GET FIRST 8 CHARS
SET "tsclean=%tsclean::=%" &:: ERASE COLONS
IF "%tsclean:~0,1%" == " " DO SET "tsclean=0%tsclean:~1,7%" &:: ADD 0 IF SPACE AT BEGINNING

REM TEST SYSTEM SHORT DATEFORMAT (NOTE! 'DELIMS=[TAB][SPACE]')
REM FOR XP AND PRIOR, 'SKIP' VALUE SHOULD BE == '4'!
REM FOR VISTA AND AFTER, 'SKIP' VALUE SHOULD BE == '2'!

FOR /F "skip=2 tokens=2* delims=	 " %%A IN ('REG QUERY "HKCU\Control Panel\International" /V sShortDate') DO SET dform=%%B

REM FOLLOW DATEFORMAT TO GENERATE A UNIQUE STRING AT "%%tduniq%%"

IF "%dform:~0,4%" == "yyyy" DO GOTO ISO
IF "%dform:~0,5%" == "MM-dd" DO GOTO MUDDY

:DUMMY
REM BOTH CONDITIONS ABOVE ARE FALSE
SET "yyyymmdd=%date:~6,4%%date:~3,2%%date:~0,2%"
SET "tduniq=%yyyymmdd%%tsclean%"
GOTO PROCEED

:ISO
REM FIRST CONDITION ABOVE IS TRUE
SET "yyyymmdd=%date:-=%"
SET "tduniq=%yyyymmdd%%tsclean%"
GOTO PROCEED

:MUDDY
REM SECOND CONDITION ABOVE IS TRUE
SET "yyyymmdd=%date:~6,4%%date:~0,2%%date:~3,2%"
SET "tduniq=%yyyymmdd%%tsclean%"

:PROCEED

MOVE %1 %tduniq% > nul
MOVE %2 %fsObjA% > nul
MOVE %tduniq% %fsObjB% > nul
ECHO %yyyymmdd%_%tsclean%: %fsObjA% name swapped with %fsObjB% > swap-%tduniq%.log
ECHO.
ECHO DONE!

ENDLOCAL
Most of it (except for the first and last blocks) is a re-purpose of prior code to generate an unique string from the Windows date/time outputs. As it uses the MOVE command, I think it will work with folders, but I was too lazy to test it. Should this prove useful to anyone, you might want to add '@ECHO OFF' at the very beginning...

Of course, Hyadaral's solution is much more streamlined and elegant, but that's not the point of this little exercise, is it? ;)

User avatar
webfork
Posts: 10823
Joined: Wed Apr 11, 2007 8:06 pm
Location: US, Texas
Contact:

Re: Do you speak Batch?

#18 Post by webfork »

Old thread update:

A very good introduction to windows batch files: https://www.maketecheasier.com/move-fil ... n-windows/

Post Reply