I am referencing the solution posted by Compo on this topic:
BATCH Find string in subfolders and move to new location
In which I used his script to retrieve only the files containing "TP" and transferring to the target folder. This works for some PCs (works on either Windows 7 or Win 10 PCs) but most other PCs (with similar OS) STILL PULL files with containing variants "TF" or "TA" in the strWord.
#Echo Off
Set "strPath=C:\temp\source\"
Set "strExtn=.tar"
Set "strWord=TP"
Set "strDest=C:\temp\target\"
CD /D "%strPath%" 2>Nul || Exit /B
echo %strPath%
For /F "Delims=" %%A In (
'Findstr /MISC:"%strWord%" "%strPath%\*%strExtn%" 2^>Nul'
) Do RoboCopy "%%~dpA." "%strDest%\%strWord%" "%%~nxA" /MOV>Nul
I have tried making similar batch files run through task scheduler to pull and sort out the files that contain strWord "TA" and "TF" as well, however on those PCs that do not work, I see that the 'Findstr' filter does not work.
May I know if there is a way to improve the findstring to read the whole strword like and explicitly look for "TP" key word and not accept the "TA" or "TF" as well?
Related
My university is using a proprietary system that outputs data in a very specific way in which each "module" is output into folders following the structure (4 digit numeral) - (name of module)
ex: 5574-CHEM104
I need to remove the name and hyphen so that only the numeral remains:
5574-CHEM104 > 5574
The problem is that there's thousands of these folders and there's no way I could do it by hand. I'm having difficulty trying to automate the process, so if anyone could at least point out a command I could look into it would help immensely
I've tried the REN command, putting "REN 5574-CHEM104 5574", but it only works for one folder. There's thousands of folders, each with different numerals, under "CHEM104", for example, and I need for the program to rename the folder no matter the original name into the first 4 original numerals, which I can't figure out. Thanks!
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "delims=" %%e IN (
'dir /b /ad "%sourcedir%\*" '
) DO FOR /f "delims=-" %%o IN ("%%e") DO ECHO REN "%sourcedir%\%%e" "%%o"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the directories.
Using a list of the directory-names, names only (/b) and directories only (/ad), tokenise the name using - as a delimiter and execute the rename using the token assigned to %%o (default is first token)
I had a bash xcopy script, but got a lot of issues. After a lot of searching and reading I guess robocopy was a better tool to do the copy.
(The script must run on windows 10 computers without installing anything else and without internet access)
I'm trying to make a bash script that copy (with robocopy) some local network folders and files to a local custom directory. The aim is to be able to access to the files off from the local network.
The path to folders and files are stored inside a txt file (each line = a path)
I want to keep the structure of folder I save locally.
For example the folder X:\Path\to some\local\network\folder\with\some & characters\ will result in C:\PathTolocalFolder\Path\to some\local\network\folder\with\some & characters\ (without the X:\ letter)
Based on many similar questions (but not all at the same time) I have done this :
#echo off
SETLOCAL EnableDelayedExpansion
cls
cd C:
chcp 28591 > nul
for /f "delims=*" %%a in ('type "X:\path with spaces & specials characters\List.txt"') do (
REM echo %%a
REM echo !%%a!
echo %%a to C:\PathTolocalFolder!%%a!
ROBOCOPY "%%~a" "C:\PathTolocalFolder!%%a!" /S /A+:RA /R:1 /W:5
)
It is partially a success, but :
As there are special characters everywhere in paths and files names, I got some issues. Specially with & characters. My double quotes doesn't solve the problem. How could I go better?
For some cases, I want to save some files but not the whole directory where they are. The full path to the file is inside the text file. But as robocopy needs to add a space between folder path and file filter I have do some manipulation. How can I detect and extract the file name when there is one to adapt the robocopy command?
I want to use an exclusion list like I was doing before with xcopy. But robocopy doesn't accept a file in input for exclusions. I tried this to extract the exclusion file:
for /f "usebackq tokens*" %%D in ("C:\path to exclusion file\exclusions.txt") do (
if NOT "!dirs!"=="" (
Set dirs=!dirs! "%%D"
else (
Set dirs ="%%D"
)
)
But doesn't really know what I am doing and how to combine with the first part.
Bonus questions I'm using the robocopy log file functionality (removed from below) is there a way to archive (by adding the date in the name for example) previous log file before creating the new one? Is it possible to remove the progress percents in the log file but to display it in the terminal instead? How to use the "/np" option for log file but not for terminal display?
It's hard to me to understand how the delayed variables are working in batch files and how the different methods to read a file or variable are working.
Any help is welcome :)
Sorry for my bad English skills
thank for having read
I am attempting to hash all or most of the files off of a machine using a batch script. What I thought would be straight forward was of course not as FCIV will not scan hidden files. I attempted to make a for loop that would scan the individual files themselves but what works in the command line does not work in the batch file.
I would go to the root of my drive and attempt this:
FCIV -r -both c:\
However I noticed that quite a few files were missing (even as admin) with most of them being hidden files.
Thanks,
Any help would be appreciated.
I use this snippet of the code for CertUtil, it might also work for FCIV
cd /d %targetDir%
for /r %%e in (*) do (
if exist "\\?\%%e" (
FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "\\?\%%e" %hashType% ^| findstr /v "certutil hash"`) DO (SET var=%%F)
echo !var! "%%e" >> %hashDatabaseOutput%
SET /A fileCount += 1
) else (
echo ERROR 1 - Failed to Hash file, File or Directory contains special characters >> %errorlog%
echo %%e >> %errorlog%
echo.>> %errorlog%
)
)
cd /d "%workingDir%"
I have a similar issue as you, I'm scanning some files in C:\ but those aren't hidden files.
EDIT: Seems that using this method makes no difference because even when the file path that is feed to FCIV, the hashing will fail because it cannot find the file. you can look at my question here https://stackoverflow.com/questions/62111957/fciv-fails-to-find-some-files-when-hashing-c-drive
EDIT2: Found the root cause, it's caused by redirection when you attempting to scan files in C:\Windows\System32. One guy explained it clearly here
The system cannot find the path specified
Since FCIV only have 32bit mode, the only solution is to use other hashing program like rHash with 64bit.
Bit of a batch file question. I imagine lots of people are having fun with shared folders appearing as "My Documents" in windows explorer.
I look after several schools, where we have the pupil home folders set in a folder called say C:\data\pupils\yeargroup...
The teachers then have access to the pupils folder on a share & can review the pupils work. With the onset of Windows 7, whereas before Yeargroup folders have had a list of the pupils names with their mydocs inside, you now see a whole load of folders reporting to be "My Documents" due to the desktop.ini located inside of them.
So, I wrote this little batch file & have been running it in c:\data on a 15 minute automated task which has done a nice job or restoring law & order:
FOR /f "delims=" %%i IN ('dir /s /b /a-d "desktop.ini"') DO attrib -s -h %%i >nul 2>nul
FOR /f "delims=" %%i IN ('dir /s /b /a-d "desktop.ini"') DO del %%i >nul 2>nul
The has worked fine up until deployment at a new site that I've just got involved with. Whereas I set up all folders without spaces:
C:\data\pupils\yeargroupx\joebloggs
the folder names at this site are something like this:
C:\data\pupils\yeargroup x\joe bloggs
I have tested running the commands manually & seem to have to run the following (from within the folder location):
dir /ash
attrib -s -h
del desktop.ini
As there are 100s of users, I obviously want to automate this. Any ideas as to how I would tweak my script? I want to do dir /ash for every folder before I change the attribute of the ini file, otherwise cmd cannot see it.
I got that script running through fudging about rather than any in depth knowledge, so some assistance would be greatly appreciated?
Use "%%i" in the command tails and then spaces and & will not be a problem.
I would like to write a script that find a specific folder named 'A' in several windows servers then copy some files into that folder.
problem is there are many different share drives in the servers and that folder could be in any of them. I end up with a big for loop and numbers of if statement under it. wondering if there is any simple way of doing it. Much appreciated.
JS
Here is a bit of my script only include two of share drives :
#echo off
cd /d %~dp0
setlocal EnableDelayedExpansion
for /f %%A in (serverList.txt) DO (
if exist \\%%A\S$\A (
robocopy sourcefolder \\%%A\S$\A /e
) else (
if exist \\%%A\e$\A (
robocopy sourcefolder \\%%A\e$\A /e
) else (
echo %%A has incorrect folder >> errorlog.txt
)
)
endlocal
pause
I suppose this is a batch (not bash) you will run multiple time, so instread of having a txt with a list of servers you would better use a list of shares that exist, you could adapt this script to produce such a list.
If this is only a one-time convenience script and you just wonder if you could do better, you could just skip the checks and do the robocopy, it will just fail if the targetfolder doesn't exist.