问题
I want a batch file to automatically find the location of a file called "file.txt". I want the batch file to print exactly word to word, slash to slash, the path of a file present on my computer!! My MAIN idea is that the file's location must be able to be found by the batch file irrespective of wherever it is on my computer.
For eg, if the directory path of the file is " C:\users\desktop\folder\file.txt ", I want the batch file to automatically find this file, copy its path and send it exactly as it is to another text file called say "location.txt". I HOPE THE QUESTION IS CLEAR NOW!!
But the following I think is only for current directory path of the batch file and not any other file.
@echo off
setlocal enabledelayedexpansion
set "mypath=%cd%"
set "stringtoreplace=toto"
(for /f "delims=" %%a in ('type test.txt') do (
set "content=%%a"
set "content=!content:%stringtoreplace%=%mypath%!"
echo !content!
))>output.txt
回答1:
When I understand your edit correctly, you try to find a file (with unknown location) and get its actual location.
The following will look for the file(s) file.txt
in C:\users\
and it's subfolders amd write the list to output.txt
:
dir /s /b "C:\users\file.txt" >output.txt
Edit
To find a file "wherever it is on the system", you have to search on each drive.
for /f "tokens=2 delims==:" %%a in ('wmic logicaldisk where "drivetype=3" get caption /value') do (
dir /s /b "%%a:\file.txt" >>output.txt
)
(drivetype=3
would be all your hard drives. If you want to check thumb drives too, add the same line a second time, but change 3
to 2
(and a third line with 5
if you have to check CD-drives too))
回答2:
I am taking a stab in the dark here because your question is unclear, but I am assuming you have a list of files in the file test.txt
which for some strange reason contains the word toto
in it. If that is the case, then this should be more or less what you want.
It will access the file, get each line and then search for toto
and replace it with nothing, then do a search using dir /s
with findstr
@echo off
setlocal enabledelayedexpansion
set "stringtoreplace=toto"
(for /f "delims=" %%a in (test.txt) do (
set "content=%%a"
set "content=!content:%stringtoreplace%=!"
for /f %%i in ('dir /b /a-d /s ^| findstr !content!') do echo %%i
)
)>>output.txt
来源:https://stackoverflow.com/questions/54163351/display-current-location-path-with-slashes-of-any-file-in-a-text-file