1

I was trying this, it'll count file's line after I copy the file's path (Shift+right click >copy as path) and put it in batch file, but.... how do I fix it?? the last \ in %path% is causing problem.

@echo off
Setlocal EnableDelayedExpansion
set /p ifilename=Enter file name:
for %%f in (%ifilename%) do (
set paath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %paath%
echo %ifilename%

for /f "usebackq" %%a in (`dir /b /s %1 "%paath%"`)  do (
  for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
pause
LoNE WoLvES
  • 198
  • 1
  • 9
  • The real problem is `set path=%%~df%%~pf` which redefines the predefined environment variable `PATH` which is a __very important__ predefined [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows) as described in full details in my answer on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) Make sure to never unintentionally redefine one of the predefined Windows environment variables output on running in a command prompt window `set`. – Mofi Jun 25 '21 at 07:59
  • The command __DIR__ has absolutely no problem to list all directories and files recursively in a specified directory of which path ends with a backslash. But `%SystemRoot%\System32\find.exe` is not found anymore by `cmd.exe` on searching for a file with name `find` after `set path=%%~df%%~pf`. I recommend to change this line to `set "FilePath=%%~dpf"`. Please open a [command prompt](https://www.howtogeek.com/235101/), run `for/?` and read the output help carefully and completely from top of first to bottom of last page. Do the same for `dir /?` and `set /?`. – Mofi Jun 25 '21 at 08:03
  • I'll be more specific, 1st I'm a newbie, 2nd is, if I input **C:\tools\xe.txt** this, ```%%~dpf``` will be **C:\tools\** so if I put this in **path** location, it says error. But if I put **C:\tools** inside path it works, so what I need is, how to remove the last "\" form whatever the path is, make this **C:\tools\** into **C:\tools** or **C:\tools\New folder\New folder\** into **C:\tools\New folder\New folder** just remove the backslash – LoNE WoLvES Jun 25 '21 at 09:38
  • **Never** overwrite the system variable `PATH`! use another name instead! The modifier `~p` always returns paths with a trailing `\ `but this does usually not harm; if it does in a particular situation, simply append a `.`, because `\abc\.` is equivalent to `\abc`… – aschipfl Jun 25 '21 at 10:22
  • The description on how you use the batch file is clear for me, but there is no description about what the batch file should do. This is usually of no problem for the batch file coding experts, but your batch file has so much mistakes, that it is unclear at least for me what the batch file should do at all. What is the goal of `dir /b /s %1 "%path%"`? There is with `%1` referenced the first argument passed to the batch file. There is nothing written about batch file being called with an argument. – Mofi Jun 25 '21 at 13:50
  • Replace in your batch file all `path` by `FilePath` to fix the main mistake without any discussion with us if the string value assigned to `path` is a problem or not. You can simply open a command prompt run `dir /b /s C:\Windows\ "C:\Program Files\"` and you will see on massive output that it is possible to specify two directory paths on one __DIR__ command line and the backslash at end is not wrong, but in real recommended on wanting a list of all files and folders of a directory and all its subdirectories. Run in a __new__ command prompt also once `set path` to see the predefined `PATH`. – Mofi Jun 25 '21 at 13:54
  • Hint: If a batch file contains something like `set /P "ifilename=Enter file name: "`, it is possible to drag a file from Windows Explorer window over the console window of command process which is processing the batch file and drop it on this window to "enter" the full qualified file name of dragged and dropped file and then just __RETURN__ or __ENTER__ must be pressed to continue batch file processing with that file name. – Mofi Jun 25 '21 at 13:57
  • ya to drag and drop file from another location and use that that file, that's why I need this – LoNE WoLvES Jun 25 '21 at 16:55

1 Answers1

0

>> the last \ in %path% is causing problem
It's easy to solve this , the code is :

set TempDir=C:\0TEMP
@echo off
md %TempDir%
cd /d  %TempDir%

::------

@echo off
@echo on
Setlocal EnableDelayedExpansion

::set /p ifilename=Enter file name:
SET DUMMYexe=%TempDir%\DUMMY.exe
IF EXIST "%DUMMYexe%" goto ll123
ECHO ---------writing
pause
(
  ECHO pause1
  ECHO pause2
  ECHO pause3
) > %DUMMYexe%
:ll123
SET ifilename=%DUMMYexe%

for %%f in (%ifilename%) do (
set fpath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %fpath%
echo %ifilename%

SET v=asdf1234
SET vv=\%v%
SET vvv=%fpath%%vv%

CALL SET v=%%vvv:\%vv%=%%
echo 111---%v%

pause
set fpath=%v%


for /f "usebackq" %%a in (`dir /b /s %1 "%fpath%"`)  do (
  for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
echo on
pause
goto

But of course, if I use the var 'path', my win10 will report :
'find' is not recognized as an internal or external command, operable program or batch file.

BTW, Maybe you'd be interested in the code below :


SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`dir `) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)
ECHO -------------- %count%
ECHO
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
pause

which I tested after copying from:
How to set commands output as a variable in a batch file

Very useful info about "Setlocal EnableDelayedExpansion" can be found in:
How do SETLOCAL and ENABLEDELAYEDEXPANSION work?

exactzen
  • 1
  • 3