-2

Im making a small script but i reached a head scratch I want the batch file to look for example.exe or Folder name in all drives then cd to it and create a txt file inside it? is that even possible :P?

cd /d D: 
dir example.exe /s /p 
lets say its found and the dir is D:/Example.exe 
so i want the batch to do this,
 if example.exe is found cd to it directory then 
REM. >> "D:/logs.txt

is that possible? –

what do i put after "if exist" for the batch file to automatically switch to the found file directory

@echo on
cd /d D: 
dir example.exe /s /p if exist (whether its in D:/Folder/folder or D:/Folder  go to directory)
echo >test.txt
pause

Finally Solution by @Stephan best working answer

@echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
  for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
    ECHO break>"%%~dpAtest.txt"
  )
)
  • ... what? explain more? – TessellatingHeckler Jun 24 '16 at 01:06
  • 1
    You have four steps: 1) Search for the file; 2) Change to that directory; 3) Create the text file. 4) Repeat 1-3 above on all other drives. So start by searching here for how to search for a specific file in all folders using a batch file (there are existing questions that show how to do so). Then, when you have that working, add step 2 and get that working (again, by searching this site for previous answers). Then you can worry about adding #3. And then you can work on step 4. But do **something** yourself first to try to figure it out - we're not a code writing service. – Ken White Jun 24 '16 at 01:12
  • Why downing my post ? seriously? @KenWhite thank you for giving tips i will work on it and see how it goes :) –  Jun 24 '16 at 09:31
  • So i figured most of it out cd /d D: dir example.exe /s /p lets say its found and the dir is D:/Example.exe so i want the batch to do if example.exe is found cd to it directory then REM. >> "D:/logs.txt is that possible? –  Jun 24 '16 at 09:39
  • That's a lot better and deserves imo some upvoting – Florian Straub Jun 24 '16 at 14:39

2 Answers2

0

Updated answer, since I got your question after this update:

In order to process all the files you found:

FOR /F "delims=#" %%f IN ('dir example.exe /s /b ') DO ( 
  echo example.exe was found at path %%~pf
)

Now you can modify your ECHO statement:

 ECHO some text > C:\logs.txt 

If you want to append to the file instead of replacing, you have to use two brackets:

 ECHO some text >> C:\logs.txt 

You can enter a path after > . You don't need to CD before.

You can also use the variable for CD:

 cd %%~pf

If you want some other part of %%f, you can do FOR /? on your command line or see the reference

Florian Straub
  • 826
  • 9
  • 18
  • See http://stackoverflow.com/a/4340395/4609258 and replace "rem file exists" by what I wrote above. Also read the comments after that answer! – Florian Straub Jun 24 '16 at 11:07
  • I dont want to enter a path i want this @echo on cd /d D: dir example.exe /s /p if exist (whether its in D:/Folder/folder or D:/Folder go to directory) echo >test.txt pause –  Jun 24 '16 at 12:22
  • I don't get your question – Florian Straub Jun 24 '16 at 13:22
  • Sir.... you awesome kissing you awesome brain :D But! the file is being created in D:/ not the same folder as the where it found the file, im sorry i seem stupid but im not that experienced with any commands. am i doing it wrong? `code`FOR /F "delims=#" %%f IN ('dir example.exe /s /b ') DO echo example.exe was found at path %%~pf echo >>debug.flag`code` –  Jun 24 '16 at 14:55
  • @999indTrap Which file are you referring to? – Florian Straub Jul 22 '16 at 20:40
  • nvm Look below found a solution, thanks for your help <3 –  Jul 24 '16 at 14:48
0

To catch the output of a command, use for.
dir /p does not make sense here (it's to pause if the output is longer than the screen). You want /b (bare format; filename only / drive/path/filename when used with /s).
%%~dpA gives drive:\path\ only.
break>filename to create an empty file (REM. does also work, but I prefer break)

@echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
  for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
    ECHO break>"%%~dpAtest.txt"
  )
)

this puts an empty test.txt to every folder in every available drive where there is an example.exe. If there already should be a test.txt, it gets overwritten by an empty file.

NOTE: this only echoes the break command to the screen (for security reason). If the output is like you want it, remove the ECHO.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you for taking the time to hlep really appreciate it, but its putting the file in D:/ not in D:\Folder\Example.exe –  Jun 24 '16 at 15:29
  • is `example.exe` a file or a folder? – Stephan Jun 24 '16 at 15:47
  • Its a file d:/Folder/example.exe when the file is found i want to batch to auto create a (file) same directory automatically without me entering or pressing anything. All the answers and whatever i did creates the file in D:/ –  Jun 24 '16 at 15:51
  • please doublecheck. This code adds `test.txt` to the same directories, where there is `example.exe` (given, you have write permission). There is no reason why it should write to `d:\\` - except there is `d:\example.exe` – Stephan Jun 24 '16 at 16:02
  • ........... I love you man :) i really do love how confident you are and you were right its working 100%.... i did a mistake copy pasting it :D –  Jun 24 '16 at 16:16
  • hard to tell without seeing your code. Please [ask another question](http://stackoverflow.com/questions/ask) for that. – Stephan Jun 24 '16 at 17:42
  • Nevermind i removed setlocal enabledelayedexpansion and its working because its like IF %a%==2 setlocal enabledelayedexpansion then the rest of the script is in a different line so its just doing the line next to IF %a%==2 and when choosing IF %a%==1 it will do 1 and then pass to after IF %a%==2 and do the delete scripts.... i dont even know if what i said even make sense.. i think im gonna give up on learning coding and programming :( –  Jun 24 '16 at 17:49
  • well - to be honest, it doesn't make any sense to me. But if you got it working... If you are just starting, take a look at powershell. Batch evolved over decades, there is no red line in syntax or "grammar" - it's more a bunch of utilities from different programmers from different times More exclusions than rules. Powershell however is "designed" from scratch, uses modern programming methods and is much more powerful than batch (well, there _are_ things that are easier with batch, but just a few). Keep up programming, it makes IT-live much easier. – Stephan Jun 24 '16 at 19:51
  • Thank you for taking the time to write to me, Yes i realized that somethings are way to easy to do with batch file but somethings i got stuck in for days trying to figure out, i will start watching tutorials and start converting the scripts i made with batch to Powershell... and i hope i will learn a thing or 2 out of it,... thank you again Good Sir, you are a good example of how to help and treat people, and i wish you the best of not luck but the best of a good efforts and a better outcome :). Im writer yes :P –  Jun 24 '16 at 20:47