#echo off
echo File Extionsion To Create List?
SET /p Ext=Ext:
echo %Ext%
for /f "tokens=1 delims=." %%g in ('dir /b *.%Ext%') do echo %%g >> Names.txt
How do you remove the last dot and the file extension without accidentally removing other dots. For example 10.01320.pdf will become 10.01320.
Since your goal is to keep all the .'s in the file name just not the extension, you can simply use some Parameter Extensions. In the example of your goal, %%~ng will Expand %%g to a file Name without file extension or path.
#echo off
echo File Extionsion To Create List?
SET /p Ext=Ext:
echo %Ext%
for /f "tokens=1 delims=*" %%g in ('dir /b *.%Ext%') do echo %%~ng >> Names.txt
Related
I am trying to get the file names using a batch file from a folder but it just doesn't work.
I followed guidelines from here but for some reason this isn't returning anything at all when it should!
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD\*.txt') DO SET result=%%G
I also tried:
FOR /F "tokens=*" %%G IN (dir /b C:\Users\Desktop\UPD\*.txt') DO SET _result=%%~G
echo %_result% >> %~dp0Outputfile.txt
What I get is:
ECHO is on.
EDIT
Here is what I did so far now:
IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt (
echo file found >> %~dp0Outputfile.txt
chDIR C:\Users\Nathanael\Desktop\UPD\
dir *.txt /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo %result% >> %~dp0Outputfile.txt
)
The output is:
file found
NewVHD.txt
random.txt
ECHO is on.
If the for /f returns multiple files the last one will overwrite the previous in the set
The way cmd.exe parses (code blocks) requires delayed expansion when a var is set and used inside the same code block as is the case with your if exist
So either avoid the code block with reversed logic
IF NOT EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" Goto :Eof or other label
Or (always indent code blocks to better keep track) :
Setlocal EnableDelayedExpansion
IF EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" (
echo file found >> %~dp0Outputfile.txt
chDIR "C:\Users\Nathanael\Desktop\UPD\"
dir "*.txt" /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo(!result! >> %~dp0Outputfile.txt
)
It's a good habit to always enclose pathes in double quotes
to avoid echo is off messages use an other command separator than a space if the var is possibly empty (I used a ( here
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD*.txt') DO SET result=%%G
Make sure that the path is correct (for example, perhaps it should be c:\Users\YourName\Desktop\UPD*.txt where YourName is the user name)?
So I'm trying to make my own dir command for cmd. So far it is working great, except I want to output the directories and files sorted by file extension, in the way
dir /o:ge
would display (folders first, then files sorted by file extension).
So far, my code looks like this
#echo off
rem Title
echo.
echo CURRENT DIRECTORY [%cd%]
echo.
rem Directories
for /d %%D in (*) do (
echo [DIR] %%~nD
)
rem Files
for %%F in (*) do (
echo %%~nxF
)
#echo on
This produces:
I'm not sure how to approach outputting the files sorted by file extension. I have searched the web and can't find a solution to this problem. I do realize batch script is very limited, but I still want to try and implement this. I have thought of using a for loop and storing all the file extensions into an "array" (if that exists in batch), and then outputting them by
*.fileExtension
Any suggestions?
Cheers,
Derek
As in my comment…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/AD/ON') Do Echo [DIR] %%A
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/A-D/OE') Do Echo %%A
Echo(&Pause>Nul
Alternatively…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Delims=" %%A In ('Dir /OGE/-C'
) Do For /F "Tokens=3*" %%B In ("%%A"
) Do If "%%B"=="<DIR>" (If Not "%%C"=="." If Not "%%C"==".." Echo [DIR] %%C
) Else Echo %%C
Echo(&Pause>Nul
I want to write a batch script to rename folders in a directory.
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
So if I have 20 folders, 20 names would exist in file to rename each folder.
What I have so far:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
The above script didn't give me the desired result.
Not tested:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
The problem is that the dir /b %old% command generate a list of files with .txt extension. If you want to rename folders, then include /AD switch and eliminate the wild-card:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
I would like to delete a file name from a txt list ussing a drag and drop function however i cant get my code to work
:delete
setlocal enableDelayedExpansion
set /p dnr=%1
find /v "!dnr!" document.txt > deleted.txt
pause
This will remove the file you drag and drop onto it, from the text file (presuming name and extension format). If it's full path format then change the %%~nxa to %%a.
:delete
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ("%1") do (
set dnr=%%~nxa
find /v "!dnr!" filenames.txt >deleted.txt
)
for /f "skip=2 tokens=* delims=" %%x in (deleted.txt) do echo %%x >>new.txt
del deleted.txt /f /q
ren new.txt deleted.txt
I'm trying to concatenate many files into two individual files.
The first file will be a concatenation of all other files with "bob" in the filename. The second file will be a concatenation of all files WITHOUT "bob" in the filename. Both files will output the name of the file before actually doing the concatenation.
Here's what I have so far:
#echo off
setlocal EnableDelayedExpansion
set bob=All_bob.txt
set jimmy=All_jimmy.txt
if exist %bob% del %bob%
if exist %jimmy% del %jimmy%
for %%a in (*bob*.txt) do (
echo /* >>%bob%
echo * %%a >>%bob%
echo */ >>%bob%
copy/b %bob%+"%%a" %bob%
echo. >>%bob%
echo. >>%bob%)
for %%a not in (*bob*.txt) do (
echo /* >>%jimmy%
echo * %%a >>%jimmy%
echo */ >>%jimmy%
copy/b %jimmy%+"%%a" %jimmy%
echo. >>%jimmy%
echo. >>%jimmy%)
However, the second FOR loop (at the bottom) doesn't want to play nice using "not", and using an exclamation point like this...
for %%a !(*bob*.txt) do (
...doesn't want to work, either. So how do I concatenate files that do NOT contain what is inside the parenthesis?
I don't think there is a clean solution to this.
You could probably use FINDSTR to filter %%a but that would require turning *bob*.txt into a regular expression and that is probably not easy to automate.
Another (ugly) solution is to use nested loops:
echo bob:
for %%a in (*bob*.txt) do (
echo %%a
)
echo not bob:
for %%a in (*) do (
setlocal ENABLEDELAYEDEXPANSION&set inc=1
for %%b in (*bob*.txt) do if "%%~a"=="%%~b" set inc=0
if "!inc!"=="1" echo %%a
endlocal
)
How about using find:
for /F %%a in ('dir /b *.txt') do (
echo %%a | find /V "bob")
This should return all .txt files that don't have "bob" in them.
Using findstr and a regular expression for *bob*.txt:
for /f "usebackq delims=" %%a in (`dir /b ^| findstr ".*bob.*\.txt"`) do (…)
Just use the /V switch to process all other files:
for /f "usebackq delims=" %%a in (`dir /b ^| findstr /v ".*bob.*\.txt"`) do (…)
You can use the help command or the /? switch for for or findstr for more information.
I cleared the delimiters (delims=) to allow for spaces in the filenames.