Need help on Getting File name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Need help on Getting File name

#1 Post by born2achieve » 16 Nov 2014 20:34

Hi,

Below is my folder path,

D:/Sample

I have .sql files in that folder and i need to get the file names based on modified/created date i pass. for ex : i need to get the file names created/modified on or after 10/01/2014(mm/dd/yyyy). Can any please give me some sample script to make this as batch.

Any help is appreciated.

thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#2 Post by foxidrive » 17 Nov 2014 02:24

Can you be clearer about what you want to do?

Do you want modified dates or creation dates?
Are there any tools that you can't use?

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#3 Post by born2achieve » 17 Nov 2014 05:50

Hi, thanks for your reply. I need to consider the both. If any file modified/Created on or after the date specified. I need to get the file name and export those names in separate text file.

Any help please. Any sample script please

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#4 Post by foxidrive » 17 Nov 2014 05:58

What OS is this for?
Is it running on a local drive or a networked drive?

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#5 Post by born2achieve » 17 Nov 2014 06:23

OS: windows7 64 bit and it's local drive

thanks

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Need help on Getting File name

#6 Post by Compo » 17 Nov 2014 06:42

Without any form of error checking does the following help?

Code: Select all

@Set/P "Mdy=Please provide date [MM/dd/yyyy] "
@ForFiles /P D:\Sample /M *.sql /D %Mdy:~3,2%/%Mdy:~,2%/%Mdy:~-4% /C^
 "Cmd /C Echo @path">"%~dp0separate.txt"


[EDIT]I've included an updated version which fulfils the later additions of requiring to search subfolders and to output without paths.

Code: Select all

@Set/P "Mdy=Please provide date [MM/dd/yyyy] "
@ForFiles /P D:\Sample /S /M *.sql /D %Mdy:~3,2%/%Mdy:~,2%/%Mdy:~-4% /C^
 "Cmd /C Echo @file">"%~dp0list.txt"
[/EDIT]
Last edited by Compo on 18 Nov 2014 11:56, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#7 Post by foxidrive » 17 Nov 2014 07:14

Test this: it uses WSH and the date format is YYYYMMDD

Code: Select all

@echo off
if "%~1"=="" (
   echo checks for creation and modified dates and creates list.txt
   echo with filenames that are later than the date given as %%1 in YYYYMMDD format
   echo/
   echo example: "%~f0" 20141001
   pause
   goto :EOF
   )


:loop
set "file=%temp%\renfile-%random%.vbs"
if exist "%file%" goto :loop

:: change the source folder and filespec here

pushd "d:\sample"
dir *.sql /b /a-d >"%file%.list"

(
 echo. Const ForReading = 1, ForWriting = 2
 echo. infile = "%file%.list"
 echo. Set fso = CreateObject("Scripting.FileSystemObject"^)
 echo. Set f1 = fso.OpenTextFile(infile, ForReading^)
 echo. Do While not f1.AtEndOfStream
 echo. Set f = fso.GetFile(f1.readline^)
 echo. ' n = f.DateLastAccessed
 echo. nc = f.DateCreated
 echo. nm = f.DateLastModified
 echo. WScript.Echo CStr(Year(nm^)^)+Right(100+Month(nm^),2^)+Right(100+Day(nm^),2^)+" "+CStr(Year(nc^)^)+Right(100+Month(nc^),2^)+Right(100+Day(nc^),2^)+" "+chr(34^)+f+chr(34^)
 echo. loop
 echo. f1.close
)>"%file%"
cscript //nologo "%file%" >"%file%.txt"

:: Gets date today
set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "today=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

:: show the list of files in this format: modified-date creation-date "filename"
:: type "%file%.txt"&pause

(for /f "usebackq tokens=1,2,*" %%a in ("%file%.txt") do (
   set "skip="
   if not %%a GTR %~1 if not %%b GTR %~1 set skip=1
   if not defined skip echo/%%c
   )
)>"%userprofile%\desktop\list.txt"

del "%file%" "%file%.txt"  "%file%.list"
pause

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#8 Post by born2achieve » 17 Nov 2014 09:30

hi Foxidrive,

thanks for your time on this post and i copied your code and made it as .bat file. when i execute the file it shows the below line on cmd
checks for creation and modified dates and creates list.txt
with filenames that are later than the date given as %1 in YYYYMMDD format

example: "D:\Sample\Sample.bat" 20140101
Press any key to continue . . .



nothing happening.

I forgot to mention one point, The directory D:/sample has lot of sub folders in it and all sub folders has some .sql files it. So Will your code searches for sub folders on the Root folder?

Any suggestions or help please

thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#9 Post by foxidrive » 17 Nov 2014 19:25

The file list.txt is on your desktop.

Change this:


:: change the source folder and filespec here

pushd "d:\sample"
dir *.sql /b /a-d >"%file%.list"


to this and try it:

Code: Select all

:: change the source folder and filespec here

dir "d:\sample\*.sql" /s /b /a-d >"%file%.list"

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#10 Post by born2achieve » 18 Nov 2014 06:14

Hi Foxidrive,

Thanks for the reply and not sure am i missing something.
Thanks for the reply

I copied your code and saved it as CreateFile.bat. Also i wiped off everything in my folder and created new sub folders and each sub folder has one .sql file. Then i executed the batch file created.

It shows the below lines on the cmd,
checks for creation and modified dates and creates list.txt
with filenames that are later than the date given as %1 in YYYYMMDD format

example: "D:\Sample\Sample.bat" 20140101
Press any key to continue . . .



This is the full code of the CreateFile.bat.

Code: Select all

@echo off
if "%~1"=="" (
   echo checks for creation and modified dates and creates list.txt
   echo with filenames that are later than the date given as %%1 in YYYYMMDD format
   echo/
   echo example: "%~f0" 20140101
   pause
   goto :EOF
   )


:loop
set "file=%temp%\renfile-%random%.vbs"
if exist "%file%" goto :loop

:: change the source folder and filespec here

dir "d:\sample\*.sql" /s /b /a-d >"%file%.list"

(
 echo. Const ForReading = 1, ForWriting = 2
 echo. infile = "%file%.list"
 echo. Set fso = CreateObject("Scripting.FileSystemObject"^)
 echo. Set f1 = fso.OpenTextFile(infile, ForReading^)
 echo. Do While not f1.AtEndOfStream
 echo. Set f = fso.GetFile(f1.readline^)
 echo. ' n = f.DateLastAccessed
 echo. nc = f.DateCreated
 echo. nm = f.DateLastModified
 echo. WScript.Echo CStr(Year(nm^)^)+Right(100+Month(nm^),2^)+Right(100+Day(nm^),2^)+" "+CStr(Year(nc^)^)+Right(100+Month(nc^),2^)+Right(100+Day(nc^),2^)+" "+chr(34^)+f+chr(34^)
 echo. loop
 echo. f1.close
)>"%file%"
cscript //nologo "%file%" >"%file%.txt"

:: Gets date today
set day=0
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)^& chr(9)
for /f "delims=" %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "today=%yyyy%%mm%%dd%"
set "TAB=%result:~-1%"

:: show the list of files in this format: modified-date creation-date "filename"
:: type "%file%.txt"&pause

(for /f "usebackq tokens=1,2,*" %%a in ("%file%.txt") do (
   set "skip="
   if not %%a GTR %~1 if not %%b GTR %~1 set skip=1
   if not defined skip echo/%%c
   )
)>"%userprofile%\desktop\list.txt"

del "%file%" "%file%.txt"  "%file%.list"
pause


Thanks

not sure why it's not giving the result. Any help is appreciated.
thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#11 Post by foxidrive » 18 Nov 2014 06:23

born2achieve wrote:Thanks for the reply and not sure am i missing something.

It shows the below lines on the cmd,
checks for creation and modified dates and creates list.txt
with filenames that are later than the date given as %1 in YYYYMMDD format

example: "D:\Sample\Sample.bat" 20140101
Press any key to continue . . .



There is a simple reason for that - you are supposed to supply the date you want to check on the command line,
and if you forgot to do that then it reminds you and shows you how to add the date.

Open a cmd prompt in the test folder and type this:

Code: Select all

CreateFile.bat 20141001


It looks like you altered that date, thinking that is where to put it.

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#12 Post by born2achieve » 18 Nov 2014 06:37

Sorry for my mistake and thanks for the reply. now i got output in desktop as file.
Quick questions:

Can i get the output file created in same directory instead of desktop?
can i get only the file name. not all the path to be outputted. any modification on the code? please

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help on Getting File name

#13 Post by foxidrive » 18 Nov 2014 06:51

born2achieve wrote:Can i get the output file created in same directory instead of desktop?

Yes, you can. With a small amount of study in the code you should see where to change it.
can i get only the file name. not all the path to be outputted. any modification on the code? please

You have files in different directories - how will you know which directory they are in?

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: Need help on Getting File name

#14 Post by born2achieve » 18 Nov 2014 08:24

Thanks and i am able output the file in same directory by the below change,
(for /f "usebackq tokens=1,2,*" %%a in ("%file%.txt") do (
set "skip="
if not %%a GTR %~1 if not %%b GTR %~1 set skip=1
if not defined skip echo/%%c
)
)>"D:\Sample\list.txt"


as i asked in my previous post, i need only he file and not the directory path. i don't need to consider the path. any help is appreciated

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Need help on Getting File name

#15 Post by Squashman » 18 Nov 2014 09:31

Change this:

Code: Select all

echo/%%c

To this:

Code: Select all

echo/%%~nxc

Post Reply