Page 1 of 2
Need help on Getting File name
Posted: 16 Nov 2014 20:34
by born2achieve
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
Re: Need help on Getting File name
Posted: 17 Nov 2014 02:24
by foxidrive
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?
Re: Need help on Getting File name
Posted: 17 Nov 2014 05:50
by born2achieve
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
Re: Need help on Getting File name
Posted: 17 Nov 2014 05:58
by foxidrive
What OS is this for?
Is it running on a local drive or a networked drive?
Re: Need help on Getting File name
Posted: 17 Nov 2014 06:23
by born2achieve
OS: windows7 64 bit and it's local drive
thanks
Re: Need help on Getting File name
Posted: 17 Nov 2014 06:42
by Compo
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]
Re: Need help on Getting File name
Posted: 17 Nov 2014 07:14
by foxidrive
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
Re: Need help on Getting File name
Posted: 17 Nov 2014 09:30
by born2achieve
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
Re: Need help on Getting File name
Posted: 17 Nov 2014 19:25
by foxidrive
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"
Re: Need help on Getting File name
Posted: 18 Nov 2014 06:14
by born2achieve
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
Re: Need help on Getting File name
Posted: 18 Nov 2014 06:23
by foxidrive
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:
It looks like you altered that date, thinking that is where to put it.
Re: Need help on Getting File name
Posted: 18 Nov 2014 06:37
by born2achieve
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
Re: Need help on Getting File name
Posted: 18 Nov 2014 06:51
by foxidrive
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?
Re: Need help on Getting File name
Posted: 18 Nov 2014 08:24
by born2achieve
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
Re: Need help on Getting File name
Posted: 18 Nov 2014 09:31
by Squashman