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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dos Batch process from win 98 to W2K and XP 1

Status
Not open for further replies.

josh3217

IS-IT--Management
Feb 18, 2004
19
US
Below is a small piece of code that when placed into a batch file, can be ran to creat a folder named with todays date. I am not sure where it came from or who wrote it, but i was wondering if anyone knew how to duplicate this same process in Windows 2000 Professional and XP Pro. I know almost nothing about DOS and this one is way out of my league.

Code:
@echo off
echo @PROMPT SET DATE=$D$_> %temp%.\temp.bat
%comspec% /c %temp%.\temp.bat > %temp%.\temp2.bat
echo.> %temp%.\temp.bat
for %%? in (e111''3B e114''3B w q) do echo %%?>> %temp%.\temp.bat
DEBUG %temp%.\temp2.bat < %temp%.\temp.bat > nul
call %temp%.\temp2.bat
echo SET DATE=%%2%%3%%4> %temp%.\temp.bat
call %temp%.\temp.bat;%date%
for %%? in (temp.bat temp2.bat) do if exist %temp%.\%%? del %temp%.\%%?
C:
CD C:\WINDOWS\DESKTOP
MKDIR %DATE%
CLS
 
It might be easier if you explained what you wanted to accomplish.

1. Create a temporary directory
2. copy all the files to that directory as a backup
3. remove X number of previous folders as out of date

or?

I must admit this line bothers me a great deal:
DEBUG %temp%.\temp2.bat < %temp%.\temp.bat > nul

My word, there is no reason to call debug for any reason.

 
the main purpose is to grab the date off of my computer, and put it into a temporary variable(%DATE%), that i can use to automatically create and name a directory. There may be better ways to do what this file does, but like i said, i would have no idea how to do it.
 
Hello josh3217,

Should no longer insist on cmd-scripting for this kind of task. Make a .vbs, named say md_date.vbs with text editor.
Code:
const root="d:\test\"  'edit this root location
fldname=date()
fldname=replace(fldname,"/","-")
set fso=createobject("scripting.filesystemobject")
on error resume next
fso.createfolder root&fldname
select case err.number
    case 58 wscript.echo "Cannot find the root."
    case 76 wscript.echo "Directory already exists."
    case else wscript.echo "Unspecified error." : err.clear
end select
on error goto 0
set fso=nothing
Edit the root to place where you want the new directory be placed.
The .vbs is executable by double-click.
If the task be a part of the .bat, then simply place the following line in the bat where you want it executed.
Code:
cscript [path\]md_date.vbs //B //nologo
regards - tsuji
 
Thank you tsuji, you are a star.
I just was completely thrown by the earlier .bat script.
This has to be the most obscure legacy .bat I have ever seen.

Bill
 
Thanks, bcastner. I agree completely with your remark on the appealing to debug in the .bat shown. Should be scrutinized very closely indeed if josh3217 decide to use/transform it.

regards - tsuji
 
I revise a bit the logic of the script to eliminate a small mistake in the part of error handling due to hastiness of putting it together.
Code:
const root="d:\test\"  'edit this root location
fldname=date()
fldname=replace(fldname,"/","-")
set fso=createobject("scripting.filesystemobject")
on error resume next
fso.createfolder root&fldname
select case err.number
    case 0 wscript.echo "Done successfully."
    case 58 wscript.echo "Cannot find the root."
    case 76 wscript.echo "Directory already exists."
    case else wscript.echo "Unspecified error."
end select
err.clear
on error goto 0
set fso=nothing
- tsuji
 
I am amazed that there was any error handling. Very nice job, tsuji.
 
well thats quite helpful, i never knew you could just make .vbs files like that. quite handy. anyone know a quick fix for formating the date as 'mmddyyyy'? I am working on it now, but it still comes out like mdyyyy.
 
josh3217,

Replace the line for fldname.
Code:
fldname=right("00"&month(date()),2)&right("00"&day(date()),2)&year(date())
Furthermore, I had mistakenly switched over the err.number 58 & 76 in the previous script, so I make a listing complete here incorporating the change in fldname requested and correcting the err.number cases. (Sorry again for the mistake!)
Code:
const root="d:\test\"  'edit this root location

fldname=right("00"&month(date()),2)&right("00"&day(date()),2)&year(date())

set fso=createobject("scripting.filesystemobject")
on error resume next
fso.createfolder root&fldname
select case err.number
    case 0 wscript.echo "Done successfully."
    case 58 wscript.echo "Directory already exists."
    case 76 wscript.echo "Cannot find the root."
    case else wscript.echo "Unspecified error."
end select
err.clear
on error goto 0
set fso=nothing

- tsuji
 
wow, this works like a charm. im a little rusty on my vbs but it is much easier to work with than DOS. I also used that original piece of code to automatically rename multiple text files. what the code below did was rename every file in the current directory to have the same file name, but with todays date appended to the end. now that i have my .vbs file working, i am trying to get this same effect. thanks again everyone for their help.
Code:
@echo off
echo @PROMPT SET DATE=$D$_> %temp%.\temp.bat
%comspec% /c %temp%.\temp.bat > %temp%.\temp2.bat
echo.> %temp%.\temp.bat
for %%? in (e111''3B e114''3B w q) do echo %%?>> %temp%.\temp.bat
DEBUG %temp%.\temp2.bat < %temp%.\temp.bat > nul
call %temp%.\temp2.bat
echo SET DATE=%%2%%3%%4> %temp%.\temp.bat
call %temp%.\temp.bat;%date%
for %%? in (temp.bat temp2.bat) do if exist %temp%.\%%? del %temp%.\%%?
REN *.txt ????????????????_%DATE%.txt.x 
REN *.x *
 
below is what i came up with, for those who may need it. it takes all the txt files in the current directory that havent already been renamed to include todays date, and addes the date to them.

Code:
todaysDate=right("00"&month(date()),2)&right("00"&day(date()),2)&year(date())

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(".")
For Each file In f.Files
  if LCase(Mid(File.Name, InStrRev(File.Name, ".")) = ".txt") then
    if instr(File.Name, todaysDate)=0 then
      fso.movefile file.path, _ 
        (UCase(left(File.Name, InStrRev(File.Name, ".")-1)) _ 
        & "_" & todaysDate & LCase(Mid(File.Name, InStrRev(File.Name, "."))) )
    end if
  end if
Next 
set fso=nothing
 
Very nice and concientous job Tsuji.
I would give you a Star but I already had.

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top