1.

Solve : working with files in dos batch?

Answer»

hello all
this is my first post
i'm new to this topic and i come upon some thing that i can't solve alone.
how can i test that a file stored in %1 ( pased as the first parameter ) actually exist ?
and if it exits how can i get it's creation date and it's last modified date so that i store them in 2 variable and comper them with the date stored in %2 ( pased as the second argument) ? The specification looks suspiciously like a school assignment.
of coarse it look es like a like a school assignment i took it from a friend's school book .
he asked me if i know anything about shell , the only thing i knew about shell is that you can manipulate an OS just like you were an user after Reading his courses i learned more but it. The exercise book seams to be more steps ahead then the courses. I tried to google for the answer but i didn't find it .
please tel me where can i read about manipulating file info using a shell in DOS.
Hello Avi & Welcome to the CH forums.

Your operating system is listed as Windows XP, to which version of Dos do you refer?

Well, pretty simple OVERALL:

if exist %1 echo File Exists!
MD temp
copy %1 temp
dir temp
pause
del temp
Quote from: Helpmeh on January 08, 2009, 06:27:55 PM

Well, pretty simple overall:

if exist %1 echo File Exists!
md temp
copy %1 temp
dir temp
pause
del temp


you didn't understand the second requirement listed.

he doesn't want to DISPLAY the creation and modified date,(of which only the Modified date is shown by DIR anyway), but rather store them into two variables, which will then be compared to the second parameter.DOS version 5.1.2006 (i'm not sure if this is write . this is the version of the cmd.exe file)Quote from: avi
DOS version 5.1.2006 ... the version of the cmd.exe file

XP command PROMPT, which is not DOS at all.
i'm running batch files from the command promt.
can please someone tell me how can i get the last modied date of a file ?
or atlist how can i get a substrig from a strig or merge to strings ? I think i can get the last modified date from the string that " dir" returns .
Quote
can please someone tell me how can i get the last modied date of a file ?

DIR /TW returns the date last written (modified). Enter DIR/? at the command prompt for all the dir options. For information on recovering filename dates etc enter FOR/? | MORE at the command prompt and check out the optional syntax.

Enter SET/? | MORE at the command prompt for information on extracting substrings.

I don't understand what you mean by "merge two strings". If you mean to concatenate strings this can be achieved by: Set Newstring=%string1%%string2%

Good luck

thanks alot
i finished the problem with file dates, it workes only for files in the same diretory as the .bat file because it recives as the first parameter only the name af the file and not a full path . i'm workin on a solution so that it can use a full path as the firt parameter , but i dont know how to extract the file name out of the path .
for ex: supuse %1 contains d:\op\avi.txt i want to store avi.txt in B (anothe var) . is there any way to do this directly ? this is just an example so %1 can contain any path .
is there a way to find out the length of a strig?

and some other probles i stepted in to

1)

set A=0123456789z
for %%i in (1,2,3,4,5,6,7,8,9) do (
set B=!A:~1,"!%%i!"!
echo %B%
)

what's wrong here? shoudn't "!%%i!" espend to it's value?

2) how can i store a spce in o a variable i tried set B=" " but it didn't work

3) how can i work with numbers ?
for ex : set A=1 set B=2 echo A+B should return 3 end echo A+1 shoud return 2
or A=A+1 ?Quote from: avi on January 10, 2009, 05:35:09 AM
thanks alot
i finished the problem with file dates, it workes only for files in the same diretory as the .bat file because it recives as the first parameter only the name af the file and not a full path . i'm workin on a solution so that it can use a full path as the firt parameter , but i dont know how to extract the file name out of the path

if %1 is d:\op\avi.txt (full drive, path, and file name)

then using the ~d ~p ~n and ~x variable modifiers:


Drive %~d1 is d:
Path: %~p1 is \op\
name: %~n1 is avi
extension: %~x1 is .txt


They can be combined for example %~dpnx1. See the FOR help for full details (type for /? at the prompt)

Quote
for ex: supuse %1 contains d:\op\avi.txt i want to store avi.txt in B (anothe var) . is there any way to do this directly ?

set B=%~nx1

Quote
is there a way to find out the length of a strig?

See below.

Quote
and some other probles i stepted in to

1)

set A=0123456789z
for %%i in (1,2,3,4,5,6,7,8,9) do (
set B=!A:~1,"!%%i!"!
echo %B%
)

what's wrong here? shoudn't "!%%i!" espend to it's value?

Code: [Select]@echo off
setlocal enabledelayedexpansion

REM make vbs script
echo Wscript.echo (Len(WScript.Arguments(0)))>stringlength.vbs

REM example
set string=Mary had a little lamb

REM Get string length
for /f %%L in ('cscript //nologo stringlength.vbs "%string%"') do set /a slen=%%L

echo String is %string%
echo String length is %slen%

REM show each character in the line
for /L %%C in (1,1,%slen%) do (
set /a n=%%C-1
call set char=%%string:~!n!,1%%
echo !char! %%C
)

Quote
2) how can i store a spce in o a variable i tried set B=" " but it didn't work

A space needs quotes.

Code: [Select]@echo off
set spac=" "
REM use ~ to remove quotes
for %%S in (%spac%) do set var=%%~S
echo hello%var%world

Quote
3) how can i work with numbers ?
for ex : set A=1 set B=2 echo A+B should return 3 end echo A+1 shoud return 2
or A=A+1 ?

You cannot do this. Batch is not like BASIC.

Use SET with the /A switch. A is for Arithmetic.
Code: [Select]set /a A=1
set /a B=2
set /a C=A+B

echo %C%

thank a lot
i modified it and this is the resoult :
it works perfectly for files directly on the drive (d:\file.ext) but it gives a strange resoult for files containg extra path.
i can't understand wat's rong
it takes the file as the first argument and the data as the second ( date is MM/DD/YYYY)

Code: [Select]@echo off
cls
setlocal enabledelayedexpansion


if exist %1 goto exista
if no exist %1 echo fisierul nu exista






:exista
set direct=%~dp1
set file=%~nx1
echo diretory is %direct%
echo file is %file%

for /f "delims=*" %%A in ('dir /s/b/tw/a-d %direct%') do (
set fdate=%%~tA
echo data E %fdate%
set fd=!fdate:~0,10!
if %%A==%file% goto data

)

:data

echo %fd%


if %fd%==%2 echo a fost modificata la data %2
if not %fd%==%2 echo nu a fost modificata la data %2


Discussion

No Comment Found