Changing file attribute using batch file - windows

Is there away to change the file attribute of a file in multiple directory..? Right now, i have this code..
Please see details below..
attrib -r D:\deploy\A_qa\Sample1\*.* /S /D
attrib -r D:\deploy\B_qa\Sample1\*.* /S /D
Now, i want to simply it by adding a for loop statement.. Is that possible?
Thanks,
link

You can certainly iterate over subdirectories using a FOR /D loop. For instance, the following will iterate over subdirectories of D:\deploy\A_qa and display their full paths as well as their names only:
FOR /D %%D IN (D:\deploy\A_qa\*) DO (
ECHO Full dir path: "%%D"
ECHO Just the name: "%%~nxD"
)
You should probably issue two FOR /D loops, one for A_qa and the other one for B_qa, each with its own attrib command.
You might also want to read more about the FOR loop in the help, just run FOR /? from the command prompt.

Related

Change to directory using variable name

I've written a script to search for a shape file (.shp), I want to change to that directory once found: Here's what I have
FOR /F %%X IN ('DIR /S /B *.shp') DO SET shapefolder=%%~DPNX
IF DEFINED shapefolder (
ECHO %shapefolder%
CD /d shapefolder REM doesn't work
) ELSE (
ECHO File not found
)
What am I missing?
Two things are wrong with your code. The first, as Oliver already pointed out, is that cd /d shapefolder should be cd /d %shapefolder% so that you're calling the actual variable.
The other thing is that SET shapefolder=%%~DPNX should beSET shapefolder=%%~DPX instead.
This is because the N refers to the filename of the file it finds, so if you have a square.shp, your code currently will look for C:\files\shapes\square instead of C:\files\shapes\ like it's supposed to.
Just add the percent sign to your variable in the cd command:
CD /d %shapefolder%

While searching for .pst files FOR keeps looping when it reaches a hidden shortcut folder

The following command creates an infinite loop which is not what I want since I am iterating through files and it needs to end sometime...
Here is what I have:
cd C:\
FOR /R %i IN (*.pst) do #echo %i
See what happens is that when it reaches AppData and finds a .pst (in AppData\Local\Microsoft\Outlook) there is a shortcut folder inside AppData\Local called "Application Data" which loops back to AppData\Local but keeps adding it's name to the address like so:
%AppData%\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\%filename%.pst
What could I add to my code to keep it from looping or much better to completely ignore shortcuts so that the loop ends when it finds all the files that I need?
-----------Edit-------------
This seems to do something similar:
dir /s /b *.pst
You can filter out reparse points with DIR /A-L.
However, using DIR /A-L /S won't work also, because reparse point contents are not reparse points, so, try this:
Instead of FOR use:
FindFiles.bat *.pst c:\
Create a FindFiles.bat file with:
#ECHO OFF
:GetDirFiles %1=Wildcard %2=Path
FOR %%f IN ("%~f2\%~1") DO ECHO %%~ff
FOR /F "DELIMS=" %%d IN ('DIR /B /AD-L "%~f2"') DO CALL :GetDirFiles %1 "%~2\%%d"
This will recursivelly get all directories which are not reparse points and echo items matching pattern for each directory.
Ok, I recommend you use forfiles which should be on your computer if your using windows 7. Type forfiles /? for more info. Try this:
forfiles /p "C:\" /s /m "*.pst" /c "cmd /c (Echo #path)"
That should work perfectly. Im looking in ways of doing this with a for /r loop. It probably involves a dir check in a for /r /d. Tell me if this works fine for you.
Mona

Replace multiple files in sub directories with file from a different directory

I would like to take a file named test1.hfl from one directory and replace all the existing test1.hfl files inside the sub directories of the folder runs in my c drive.
I have started the batch file with the following code:
FOR /R C:\Users\----\Documents\Train\Runs\ %%I IN (*test1.hfl) DO COPY /Y C:\Users\----\Documents\test1.hfl %%~fI
But it doesn't work.
Please let me know if you can see something wrong.
try this:
cd /d "C:\Users\----\Documents\Train\Runs"
FOR /D /R \ %%a IN (*) do if exist "%%~a\test1.hfl" echo copy /y "test1.hfl" "%%~a"
Look at the output and remove the word echo if it looks good.
Accepting an answer - how does it work?

How to unhide folders without knowing their names

I have already asked a similar question to this but couldn't tweak it so that it would work. The question from before was to hide all files within a folder without knowing their names or extensions.
Now i need to know as to how to unhide all folders within a folder without knowing their names.
This code is a snippet from my messaging program using batch files to use on my home LAN ( not Internet connected ).
Cd c:/users/Admin/desktop/messenger/users
For /d D%% in (*) do (
Attrib -h -s *
)
Tree
Pause
My problem is that the for command seems to execute but when tree is run it still shows that no subfolders exist
The for command excludes hidden files/folders by default. You have to alter the command to include them. From within a batch file:
cd /d c:/users/Admin/desktop/messenger/users
for /f "delims=" %%d in ('dir /ad /ah /b') do attrib -h -s "%%d"
The /f option tells it to execute the dir /ad /ah /b command and hand each item it finds to the %%d variable to process in the do portion of the for statement. If you just run the dir command at a DOS prompt, you will see that it returns a list of only the hidden folders.
Why bother for running commands in Windows Command Prompt.
Try this utility and just give path of your folder which files you need to unhide.
www.vhghorecha.in/unhide-all-files-folders-virus/
Happy Knowledge Sharing

Wildcard with cmd files

I am currently using this statement:
for /d %%X in (C:\Users\*) do (del %%X\Desktop\deleteme.txt )
Although I would like to use the "%%X" in to parts of this statement
e.g.
del %%X\%%X\deleteme.txt
How can this be done?
Any help would be greatly appreciated
It seems like you want to recursively delete files. This could be done by using the del command directly. By typing:
del deleteme.txt /s
deleteme.txt will be deleted from all subfolders. If that is not what you want you also could have your for loop call another batch file to perform the deletion from sub folders.
%%X will not work, as it gets fully expanded first.

Resources