0

I'm trying to improve a program I made and I want the working-file (the file the user wants to work on/want information about/ etc.) to be variable/not determined right from the start by the code/the program as well as the working-directory.

I managed to get the working-directory to be variable (code below), but only where the bat file is (so for this to work the user would have to copy the bat file and place it in the folder it wanted it to work). I wanted it to be possible for the user to write the directory address.

I managed to get the directory to be variable (by user's input) (code below).

set /p "source_folder=Write folders directory: "
echo %source_folder%

Now, for the file, I don't really know how to properly do it. The code below gives errors (obviously), but could it perhaps still partially used and there is only missing some code to make it work?

set filename=%%~nxi
set extension=%%~x1

nxi /d%filename%
x1 /d%extension%

echo The working-file is
echo %filename%%extension%

Or perhaps there is something much simpler, a very simple command that even a beginner should already know that I do not know? (all this is just a small part of the program, in another question I made, I have the full program Program to find duplicated files and eliminate them in a certain folder plus its sub-folders )

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Expansion is simple `%%~dpnxi` expands each item `d`rive, `p`ath `n`ane and e`x`tension. Where `i` is simply the metavariable ID you gave the loop and where `%%~1` would be for stdin instead of metavariable. Run `set /?` and `for /?` from `cmd.exe` and read the help for more – Gerhard Jul 12 '22 at 18:23

1 Answers1

0

To allow the user to enter input for a directory name, add such a line:

set /p "Source_folder=Enter the directory path: "

Then, do this again for the filename:

set /p "File_name=Enter a file name to search (without extension): "

And another line for the type:

set /p "File_type=Enter a file type to search (for example: docx): "

Then you will perform the search in this way:

for %%a in ("%source_folder%\%File_name%.*") do @echo %%~Ta %%a
for %%a in ("%source_folder%\*.%File_type%") do @echo %%~Ta %%a

The computer will search the source folder for the name of the entered file (of any type), as well as any file of the entered type.

yonni
  • 248
  • 2
  • 11
  • What about setting the file name and extension? Is it the same way? Like : set /p "filename=Write the file and its extension you want to work with: " echo This is the file echo %filename% But i also believe i would need perhaps something for the extension? – 1000Muchachos Jul 19 '22 at 08:38
  • @1000Muchachos exactly like that: `set /p "filename=Write the file and its extension you want to work with: "`. You can also get a list of all the files in the selected folder Using the `dir /b "%Source_folder%"` Or work with them all in the for loop: `for %%f in ("%Source_folder%\*") do "%%f"`. But I did not understand your question about the extension. – yonni Jul 21 '22 at 09:55
  • sorry, what i meant with extension is like if it is ".docx" or ".jpeg" or ".web", ".bat" etc. I actually have it like this " (for /f "delims=" %%a in ('dir /a-d /t:w /s /b "%source_folder%"*%filename%') do @echo %%~Ta %%a)" what it does is it lists every file from oldest to most recent in that specific folder and sub-folders that have the same name (basically a way to find duplicated files). Was not managing to make it work like this. Worked before when the file was not variable and in the source code i was searching for a certain file (don't know if im making myself clear) – 1000Muchachos Jul 22 '22 at 09:23
  • @1000Muchachos Something in your syntax is not clear to me. Are you trying to narrow down the file view to a specific user defined type? You could ask the user to enter an **extension** (instead of asking for the file name) and apply it like this: `for /f "delims=" %%a in ('dir /a-d /t:w /s /b "%source_folder%\*.%Ext%"') do @echo %%~Ta %%a` – yonni Jul 24 '22 at 13:28
  • Sorry once again ahahaha Basically i just want for the user to input the name of the file it wants to search. And i want the program to not only search by name but also by the type of file, e.g: i want to search for "Theworld.docx" or "birthday.jpg".... My problem is that i tried but instead of that file it identifies every file on the folder it is aiming to. – 1000Muchachos Jul 25 '22 at 09:18
  • @1000Muchachos Check the answer after the edit, if that's what you meant – yonni Jul 25 '22 at 10:19