How to list down hidden files using a .BAT File? - windows

For my computer project, my teacher requires us to make a bat file that lists down hidden files. The whole class doesn't know how though. Here is my code so far:
#echo off
set /p UserInput=What directory would you like?
cd %UserInput%
dir /S /aH
Pause
My teacher says I'm almost done, I'm just missing 2 or 3 characters
after "dir /S /aH". Would anyone know what these missing characters are?
Hi guys, I'm sorry for the late reply. We're having so many projects before the semester ends. :( When I run the bat file, it shows the following.. Here's the link: imgur.com/W4Wm4vP

Whilst you've already received responses that are sufficient to your initial request, I've added one just to show that you should really try to perform some sort of verification of user input before proceeding with your listing:
#ECHO OFF
:LOOP
CLS
SET /P "UserInput=What directory would you like? "
IF "%UserInput%"=="" GOTO LOOP
IF /I NOT "%CD%"=="%UserInput%" (PUSHD "%UserInput%" 2>NUL||GOTO LOOP)
DIR /B /S /AH-D
TIMEOUT /T -1 /NOBREAK
I trust this will give you a few more things to research and learn.
Edit
As an after thought, if the end user is inputting only a directory name and not a full path then you may need to replace line six with:
FOR %%A IN ("%CD%") DO IF /I NOT "%%~nxA"=="%UserInput%" (
PUSHD "%UserInput%" 2>NUL||GOTO LOOP)

Related

Windows command FOR in batch file, specified to work in a certain folder

Normally I can specify a folder for a batch file to work in.
Not so with the FOR command:
for %%a in (G_*.txt) do ren "%%a" "test-%%a"
This finds G_*.txt in all files and renames those by putting test- in front of the filename.
I tried specifying G_*.txt further with C:\test\G_*.txt but that is not accepted.
I also tried pouring this into a variable but that also failed.
Who knows what to do?
Again, I had to change the approach: using SET /R will put a long path into the %%a variable which makes the idea unsuited to put a bit of text in front of a filename.
I found the solution by selecting the work directory first and let the FOR construction do its work. Since it must be network proof, I had to use the PUSHD command.
This is what the final result looks like:
set source=\\nassie\home\test\source with nasty space
set target=D:\target
PUSHD %source%
:: If this fails then exit
If %errorlevel% NEQ 0 goto:eof
for %%a in (G_*.txt) do xcopy "%source%\%%a" "%target%\textblabla_%%a*" /D /Y
POPD

How to write a batch file to delete multiple specific files from different paths?

Each time before I start debugging, I need to delete some specific files from different paths. It's a tiresome process as I do it like a million times in a day. So I want to write a batch file that deletes all those at once without prompting.
The first path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\DD\servers\DefaultServer\tmp\
I want everything under this temp folder gone. Without the temp folder itself, of course.
The second path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\o.j2ee\drs\
I again want everything under this drs folder gone. Again without the drs folder itself, of course.
The third path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\
This time I want to delete only the files with .lok extension under the systemx folder.
I tried to write something like this:
del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\DD\servers\DefaultServer\tmp\*.*?" /s
& del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\o.j2ee\drs\*.*?" /s
& del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\*.lok"
However it doesn't meet my expectations, it doesn't work.
I appreciate all the help. Thank you very much.
Perhaps something like this would do what you want:
#Echo Off
Set "srcPath=%AppData%\JDeveloper\systemx"
Set "tmpPath=DD\servers\DefaultServer\tmp"
Set "drsPath=o.j2ee\drs"
CD /D "%srcPath%" 2>Nul || Exit /B
Del /F /Q /A *.lok
For /D %%A In ("%tmpPath%\*" "%drsPath%\*") Do (RD /S /Q "%%A"
Del /F /Q /A "%%A\*.*")
I have used the paths you provided in your question, if those have changed, you can alter them by editing lines 2, 3 and 4 as necessary.

batch delete with directory variable - windows 7

I use the following batch file to delete unwanted files on several drives.
set /p DELPATH=<"C:\DELETE-ALL.txt"
for /f "usebackq delims=;" %%i in ("C:\DELETE-ALL.txt") do #del /q "D:\HFI\%DELPATH%\%%i" > C:\DELETE-ALL-4.txt 2>&1
... same command for other local and network drives.
The DELETE-ALL.txt looks like this:
mydirectory
TEST.xlsx
TEST2.xlsx
This works great. It deletes files in single directory. But now I need it to do more. I need the batch file to delete files in different directories. So, it boils down to how to change directory on the fly.
Any help will be greatly appreciated.
I answer you here because i can't comment now with my lower reputation.
I strongely recommend to use PowerShell or python or others program scripts to do this. Using windows batch, it will take you more time to find a good way and there may be no way to do such a little complex misson.
The answer turns out to be easier than I thought. Although my original question was for deleting files, I got it to work for rename. It should work for delete with little modification.
#(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:\RENAME-ALL.txt"') do (
#echo "%%A" | find /i "\"
#if errorlevel 1 (
RENAME "%%A" "%%B" >> C:\RENAME-ALL-4.txt 2>&1
) ELSE (
CD /D D:\mydirectory\%%A
)
)
)
The script looks for "\". If found, it assumes that line is a directory and change to the corresponding directory with "D:\mydirectory\" as a path prefix. Otherwise, it assumes the line contains file name. Since back slash is not allowed in filename, the assumption is safe.
Hope this will help other people.

Windows batch: REN does not halt even if errorlevel 1

I'm using REN to find files with a certain naming pattern and modify them, like so:
REN "?%var1%?%var2%.S16" "?%var1%?%var3%.S16"
This finds all files like aXaY.S16, bXaY.S16, cXbY.S16 (etc) and renames them to aXaZ.S16, bXaZ.S16, cXbZ.S16 (etc). If it finds what it's looking for, it works just fine. But there's a problem: REN won't halt the operation if it encounters an error.
To prove this is the case, my script is as follows:
#echo off
set /p var1=Enter first var:
set /p var2=Enter second var:
set /p var3=Change second var to:
echo Searching for all files matching ?%var1%?%var2%.S16
REN "?%var1%?%var2%.S16" "?%var1%?%var3%.S16"
echo Errorlevel: %errorlevel%
IF ERRORLEVEL 1 goto :FAIL
echo Success!
PAUSE
goto :eof
:FAIL
echo I done goofed!
PAUSE
exit
I ran this in a folder containing a few hundred files. I searched for files matching ?0?a.S16 (of which there are ~40 results) and asked it to change the 'a' to a 'c', knowing that files with this name already exist and should create a conflict.
Here is the console output (shortened for brevity):
Enter first var: 0
Enter second var:a
Change second var to:c
Searching for all files matching ?0?a.S16
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
...(etc)...
Errorlevel: 1
I done goofed!
Press any key to continue . . .
The complaint about duplicates goes on for about 40 lines, as expected. As you can see, though, Errorlevel returns 1 at the end of the process instead of quitting at the first sign of trouble, which is what I'd rather it do.
I did consider passing this through FOR /f but I don't know how I would also pass the searchmask through it. I know FOR supports '*' wildcards, but as far as I'm aware, it doesn't support '?' the same way REN does. A possible alternative would be to use regular expressions somehow, but I can't wrap my head around them at all despite trying.
Any clues? Many thanks for taking a look.
Yes, you have documented how the REN command works - it continues to completion, even after a rename fails, and then reports ERRORLEVEL 1 if at least one rename failed.
If you want to halt processing upon the first error, then you will have to write your own loop to rename each file, one at a time. You should not use the simple FOR loop because it can begin iterating before it has scanned the entire directory, so you run the risk of renaming the same file twice. The safe thing to do is use FOR /F coupled with DIR /B /A-D instead.
#echo off
set /p var1=Enter first var:
set /p var2=Enter second var:
set /p var3=Change second var to:
echo Searching for all files matching ?%var1%?%var2%.S16
for /f "eol=: delims=" %%F in (
'dir /b /a-d "?%var1%?%var2%.S16"'
) do ren "%%F" "?%var1%?%var3%.S16" || goto :break
:break
echo Errorlevel: %errorlevel%
IF ERRORLEVEL 1 goto :FAIL
echo Success!
PAUSE
goto :eof
:FAIL
echo Errorlevel: %errorlevel%
echo I done goofed!
PAUSE
exit
Note - The wildcard rules used by REN are not at all intuitive. You should have a look at How does the Windows RENAME command interpret wildcards? to make sure you are getting the results you expect.
replace you ren command with
FOR /f "delims=" %%x IN ('dir /b /a-d "%sourcedir%\?%var1%?%var2%.S16" 2^>nul') DO REN "%sourcedir%\%%x" "?%var1%?%var3%.S16"&IF ERRORLEVEL 1 ECHO failed AT %%x&goto :EOF
note This is a direct patch of my test, where I set sourcedir to a testing directory. In your case, you'd need to make appropriate adjustments.
This makes a directory list in basic form without directories and each filename is then assigned to %%x Then rename is then attempted and the resultant errorlevel interpreted. On fail, show the name for good measure and bail out.
[edit] 2^>nul added into dir command.
This will cause dir errors (like "file not found") to be directed to nowhere. The caret (^) is required to tell cmd that the > is part of the dir command, not the for.

Renaming Files With Batch

I am new completely new to batch and have used Python, Java ect for about a year now. So as you can guess I dont know overly large amount.
I have been instructed by my employer to try and automate some tasks we do on a regular basis. This may sound like a stupid task that is easy to-do but it happens a lot and when talking to customers at the same time it get boring quick. The task: Renaming multiple folders... We do this when end users have forgotten their password or need a profile reset.
The code that I have at the moment is:
:again
#echo off
set /p answer= Rename a file/folder(Y/N)?
if /i "%answer:~,1%" EQU "Y" goto yes
if /i "%answer:~,1%" EQU "N" goto no
goto again
:yes
set /p dirOfFile = Directory of file:
set /p currentFolder = Name of folder to be re-named:
set /p newFolder = What will it be re-named to:
ren %dirOfFile%\%currentFolder% %newFolder%
:no
exit
The code is based off of a few sources, the Y/N and using variable from other StackOverflow posts and the ren %dirOfFile%\%currentFolder% %newFolder% from the cmd command of ren or rename.
When I use:
ren C:\Users\Zak\Desktop\TestFolder AlsoTest
It renames a folder on my desktop from TestFolder to AlsoTest. This is what I want the batch file todo, but once its working, on multiple folders.
Thanks for any/all help!
you don't have a parameter %dirOfFile%. But you have a parameter named %dirOfFile<space>%
Same with you other variables.
Write your setcommand without spaces:
set variable=value
for example:
set /p dirOfFile=Directory of file:
Simple You can rename any File with Batch
type the following in batch file
ren "oldfile.txt" "newfile.txt"

Resources