search file using bat (no user input)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xhai
Posts: 39
Joined: 13 Jun 2013 09:33

search file using bat (no user input)

#1 Post by xhai » 09 Mar 2014 08:52

Hi guys how do i code this in .bat?

i want to search for a file without user input..

after that do a command something like this...

set file = 1.txt
echo your file is %fie%

What if i delete "1.txt" and replace it with any name with .txt extension the program will not work..

is this possible auto detect if there is a new .txt and replace it to the set command

I hope you understand what i'm saying...

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

Re: search file using bat (no user input)

#2 Post by foxidrive » 09 Mar 2014 10:06

I've checked a couple of your recent threads - and you should consider following up in the threads when people reply to you, just to say "thanks" or "it works" or something.

penpen
Expert
Posts: 2002
Joined: 23 Jun 2013 06:15
Location: Germany

Re: search file using bat (no user input)

#3 Post by penpen » 09 Mar 2014 12:21

Sorry, there is much too few information:
xhai wrote:i want to search for a file without user input..
Where do you search:
- A specific directory, or
- a specific directory and all its subdirectories, or
- all drives (improbable but not impossible).
xhai wrote:What if i delete "1.txt" and replace it with any name with .txt extension the program will not work..
How do you know with which it is replaced:
- Is it the only other txt file in a specific directory, or
- is it defined by other specifications?

xhai wrote:is this possible auto detect if there is a new .txt and replace it to the set command
If we know where to search, and for what we are searching then yes, else no.

penpen

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: search file using bat (no user input)

#4 Post by pieh-ejdsch » 09 Mar 2014 14:18

Within a for /f command you use

Code: Select all

dir /a-d /o-d *.txt

to Set the newest file into Var.
Then you can rename Var.

Phil

xhai
Posts: 39
Joined: 13 Jun 2013 09:33

Re: search file using bat (no user input)

#5 Post by xhai » 11 Mar 2014 05:52

pieh-ejdsch wrote:Within a for /f command you use

Code: Select all

dir /a-d /o-d *.txt

to Set the newest file into Var.
Then you can rename Var.

Phil


Thanks you Phil...
But how do code it in for /f loop it..

foxidrive wrote:I've checked a couple of your recent threads - and you should consider following up in the threads when people reply to you, just to say "thanks" or "it works" or something.


Sorry if i didn't say thanks.. all of them works now..
Thank you for the feedback and sorry again foxidrive next time i won't forget to say thanks..
do we have a thanks button?

penpen wrote:Where do you search:
- A specific directory, or
- a specific directory and all its subdirectories,
penpen


thanks penpen

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

Re: search file using bat (no user input)

#6 Post by foxidrive » 11 Mar 2014 19:38

Please do not use PM's for personal support. Write your questions in the forum.


xhai wrote:the program accept blank or when i press enter without writing any data...

thank you and keep up foxidrive...

Code: Select all

:Start
cls
setlocal enableDelayedExpansion
color 07
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo ºThis supports only rip ISO format!                                º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Please enter the name of your ISO
echo.
color 03
set "ISO="
set /p "ISO="
for /F tokens^=1*^ delims^={}[]:/\^;^'^,~^`@#$%&^(^)+^=\/:*^?^"^<^>^| %%a in ("_!ISO!_") do if NOT "%%b" == "" goto :Start
if NOT "!ISO:*.iso=!" == "" set "ISO=!ISO!.iso"
if "%ISO%"=="" goto :Start
goto :Output

penpen
Expert
Posts: 2002
Joined: 23 Jun 2013 06:15
Location: Germany

Re: search file using bat (no user input)

#7 Post by penpen » 12 Mar 2014 02:45

foxidrive wrote:
xhai wrote:the program accept blank or when i press enter without writing any data...
(...)
The batch file you use, produces " .iso" on space input, and ".iso" on empty input.
Although unusual, both are valid file names, so there is no problem in accepting these inputs.
You may check it by creating them in the actual directory:

Code: Select all

> " .iso" echo(test
> ".iso" echo(test

penpen

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

Re: search file using bat (no user input)

#8 Post by Squashman » 12 Mar 2014 06:41

xhai wrote:the program accept blank or when i press enter without writing any data...

thank you and keep up foxidrive...

Code: Select all

:Start
cls
setlocal enableDelayedExpansion
color 07
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo ºThis supports only rip ISO format!                                º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo Please enter the name of your ISO
echo.
color 03
set "ISO="
set /p "ISO="
for /F tokens^=1*^ delims^={}[]:/\^;^'^,~^`@#$%&^(^)+^=\/:*^?^"^<^>^| %%a in ("_!ISO!_") do if NOT "%%b" == "" goto :Start
if NOT "!ISO:*.iso=!" == "" set "ISO=!ISO!.iso"
if "%ISO%"=="" goto :Start
goto :Output

What does this have to do with your original question?

Post Reply