VB script to list folders and contents - vbscript

I would like to write a VB script that will:
List all folders named "abc", and also there contents, in share drive \share1
Currently these folders named "abc" are located all over the place in \share1, but they all have the same name.
The issue I have is that some of these paths are long paths, hence getting this working through PowerShell has been very difficult, as it keeps hitting the 260 limit.

Prefixing a path with \\?\ allows paths up to 32,000 odd characters. This allows apps to opt in to very long file names as they crash most programs. Now it depends on how you access the file functions if the component will allow you. CMD does support this.
so a command line
for /f %A in ('dir c:\windows\abc /s /b /ad') do dir "\\?\%A"
\\?\ works with unc paths too.
for /f %A in ('dir \\?\c:\windows\abc /s /b /ad') do dir "%A"

I think that RoboCopy (the Windows utility) is your best bet. This can handle long path names, and it can actually be used to just list files, rather than copy them, by using the
/L option (: List only - don’t copy, timestamp or delete any files.)
http://ss64.com/nt/robocopy.html provides a good summary of all the options.
You can also download a GUI for it which might make it a bit easier to work with.

Another option is to shorten the path via subst:
subst X: C:\very\...\long\...\path

Related

Bash robocopy from text file containing path to folders and files to copy with special characters

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

Batch file to copy files

Need to have a batch file that will actually check if the file name has "abc" in its name and if its there , then it will initiate a copy.
So suppose folder /test has 5 files
apt.text
mar.text
may.text
Jan.text
abc_xyx1234.text
So since "abc" appears in string only in the last file , it should pick only last one.
I tried
#echo off
for /f %%a in (' dir /b /a-d ^| find /v /i "abc" ') copy "%%a"
pause
but it didn't work out.
You have no destination for your copy.
The find /v /i will find those filesnames that do not (/v) match abc
It's probably easier to use
copy *abc* destination
I think part of your problem is the following:
Even though forward slashes work sometimes, Windows' canonical path separator is the backslash \. Using forward slashes as path separators confuses programs which use the forward slash as parameter prefix, and most if not all of the Microsoft Windows tools do so, including FIND and FINDSTR.
Also, if you're talking about /folder - which should be \folder as explained above - you're talking about a directory in the root of the current drive. I'm not sure you really mean that.
Apart from that, Magoo's suggestion is still kind of valid, but it omits the path "problem".
So let's use absolute directories to clarify:
I'll assume your source directory is c:\work\source and contains the file set you've stated in your question. I'll assume your target directory is c:\stackoverflow\target. So you can do the following from anywhere on your system:
copy c:\work\source\*abc* c:\stackoverflow\target
If you're doing this from an arbitrary folder on drive c, you could omit the drives:
copy \work\source\*abc* \stackoverflow\target
And, if you're inside the source folder, you could use Magoo's answer:
copy *abc* \stackoverflow\target

How would I copy all files and folders except for hidden ones?

On my drive S:\ I have a folder X which has multiple files and subfilders, each of which in turn contains its own files and subfolders and so on. Each folder(at any level) contains a hidden folder named the same way, say HID, with several files in it.
I have the same structure in another drive D:\ - same folder X with the same structure, but with slighly different contents in the files.
Basically I need to copy-and-replace the contents of X from S:\ into D:\, but not touch the hidden folders hamed HID (basically, they are unique in two independent ways - by the fact that they are named HID and by the fact that they are hidden).
I'm lazy to do this manually and don't feel like writing a C++ application to do this either. Is there any easy way to do this using a small bat file or a direct shell command with smart arguments?
You may be interested in xcopy command. As it says, "By default, xcopy does not copy hidden or system files.". It also has an exclude option, which seems to be used for ignoring specially named files.
Try this: lose the echo and pause if it's echoing the right commands.
#echo off
cd /d "s:\folder x"
for /f "delims=" %%a in ('dir /a-d /b /s ^|find /v "\HID\" ') do (
echo copy /y "%%a" "d:%%~pnxa"
pause
)

Find a program and run it through command prompt

I want to be able to locate where the exe of a (third-party) program I remember from only its name. (say photoshop.exe) Is that possible?
Like you can call taskmrg.exe
try this to find:
dir /s c:\photoshop.exe
..and also to run:
for /f "delims=" %a in ('dir /b /a-d /s "c:\photoshop.exe"') do "%~fa"
Yes you can do that of course, but it involves searching for the file recursively in every folder of your hard drive partition(s).
Another way is to add the folder containing the .exe you want to execute in the PATH environment variable but it's not recommended to add every program you own to the PATH. (It's meant to remain quite small in size).
Here's the syntax of FIND command in dos.
http://www.computerhope.com/findhlp.htm
taskmgr.exe can be executed from any path at the command prompt, because its location falls within the directories listed in the %PATH% variable. You can potentially add any additional folders you would like to this variable to make any applications within follow the same behavior. Caveat: Some applications do not run properly unless started from within their home directory.
Editing the %PATH% variable via the GUI varies between versions of Windows, or you can edit it from the command prompt.

DOS command to Copy a file from parent dir to specific folder within many child directories

I'm trying to teach myself some simple DOS commands and have used relatively simple commands to copy or move files, however this specific request is presenting a challenge for me and would appreciate some expertise from this forum.
C:\Parent\library.eds (location of my source file)
Any time I update library.eds in the parent directory, I would like to copy that file into every Child directory that contains a folder named "LIB". I have standardized the Child directories to the following:
C:\Parent\Child1\INPUT
C:\Parent\Child1\OUTPUT
C:\Parent\Child1\LIB {paste library.eds here}
C:\Parent\Child2\INPUT
C:\Parent\Child2\OUTPUT
C:\Parent\Child2\LIB {paste library.eds here}
and loop through until all children with LIB directories contain the updated file "library.eds"
Thank you for your help!
Mark
Here's a command that can get you started:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO #ECHO COPY "C:\Parent\library.eds" "%%~D"
Once you get it working the way you want, remove the #ECHO part to actually do the copy:
FOR /F "delims=" %%D IN ('DIR /b /a:D /s C:\Parent\LIB') DO COPY "C:\Parent\library.eds" "%%~D"
Extra help for these commands
HELP FOR
HELP DIR
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
This lets you execute command, and loop over its output. Each line will be stuffed into %variable, and can be expanded out in otherCommand as many times as you like, wherever you like. %variable in actual use can only have a single-letter name, e.g. %V.
"delims="
This lets you ignore any whitespace output by 'command', so it properly handles directories that have spaces in their names.
DIR /b /a:D /s C:\Parent\LIB
This will search for all files under C:\Parent that have the name LIB. It will recursively go through subdirectories because of /s. It will only find directories (because of /a:D). It will output them in a way that is useful for looping, because of /b.
%%D instead of %D
This is required in batch files. If you did this on the command prompt, you would use %P instead.
if you know all the child directories before hand the simplest solution is batch files tutorial on batch files
I dont know how loops work in ms dos but in linux bash shell loops are simple to program..
Any ways batch files are the simplest option
if you can download cygwin then you can use the following bash script to do the job without hardcoding it
The code
P.S you will need to change the name of the folder from tmp to LIB and the filename from LOL to ur file name .. The script is pretty self explanatory.
Unix shells are better than MS-DOS so it might be a good idea to get cygwin anyways

Resources