Copying files from Snapshot View - batch-file

I am trying to copy files from snapshot view using the cmd code
cleartool find . -all -type f -version "lbtype(%LABEL%)" -exec "cmd /c echo f | xcopy /f /y \"%%CLEARCASE_XPN%%\" \"%STAGING_FOLDER%\%LABEL%\%%CLEARCASE_PN%%\""
But while copying files I need to copy only after a certain folder from snapshot view like
eg: DEV_VOB\LOG##\main\DEV1_Integration\2\ I want to copy files only after this.
I did try /EXCLUDE in xcopy but not working.
Need help with this.

The easier way is to include a grep before the xcopy:
cleartool find ... | grep "DEV_VOB\LOG##\main\DEV1_Integration\2\" && xcopy ...
If the grep succeeds, then the second part of the command after the && will be executed.
If the grep fails, then the second part (xcopy) won't be executed.
You have various sources for a grep on windows, like gnuwin32.sourceforge.net.

Truthfully, this won't work. You're trying to use a version extended path in a snapshot view. Those will not work.
There is a command that allows you to extract arbitrary versions from the repository and place them in another arbitrary location: cleartool get
Something along these lines may work:
cleartool find ... -exec "cleartool get -to \"%STAGING_FOLDER%\%LABEL%\%%CLEARCASE_PN%%\" \"%CLEARCASE_XPN%\""

Related

How to execute a bat file from cygwin bash that uses the Windows find command

The find command does completely different in Windows and Unix. On Windows it is a fgrep-like utility listing matching lines in a file; on Unix -- and on Cygwin -- it list filenames matching some criteria.
Cygwin bash prepends its standard directories to the current path, so inside bash $PATH is typically /bin:/usr/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS.
Begin Update to provide additional details
For example I have many scripts that use the gnu find command -- for example scripts to purge directory trees that contain no files:
purge-empty-dirs.sh
find . -depth -type d -empty | xargs rmdir -p
I also have a bat file to start my build, which uses the windows find command, which searches for lines matching a string (similar to gnu grep).
build.bat
...
dir | find "target"
if errorlevel = 1 goto no_target_dir
...
Now, for my bash script to work I need /bin to be in path before c:\windows\system32. But for my bat file to run I need c:\windows\system32 to be in path before /bin
In general we might be able to claim that all bat files should be executed with the original environment inherited by bash, not the modified one. Does that make sense?
End Update
This how it should be, but breaks the bat files executed from bash. What is the best way to address this?
Is there a way to force Cygwin to execute bat files (or even all Windows executables) with the environment it started with? I am thinking of start /i behavior of cmd.exe. I am thinking of writing my own cygstart like utility that does this, by saving the environment (or at least $PATH) in .bash_profile/.bashrc. Does that make sense?
Any other suggestions?
Edit: See also Start new cmd.exe and NOT inherit environment
If you chmod +x your.bat it works.
./your.bat
is run via cmd /c
Already answered in Why is it that Cygwin can run .bat scripts?
I've tested a solution from here and it works nicely:
start "clean shell" /i "%windir%\explorer.exe" "\path\to\your\script.bat"
Example
to-run.bat script:
echo %PATH%
echo "TODO: some commands"
cmd
cygwin bash script, run-me.sh:
#!/bin/bash -e
set -x
SCRIPT_WINPATH=`cygpath --windows --absolute "$1"`
EXPLORER_CYGPATH=`which explorer`
EXPLORER_WINPATH=`cygpath --windows --absolute "${EXPLORER_CYGPATH}"`
cmd /C start "clean shell" /I "${EXPLORER_WINPATH}" "${SCRIPT_WINPATH}"
test:
chmod +x to-run.bat
chmod +x run-me.sh
./run-me.sh to-run.bat
You can use a wrapper, say, launcher.bat, whose contents is
#echo off
setlocal
rem modify here based on your needs
set PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
call %*
endlocal
exit %errorlevel%
Say you want to call cmdFUBAR.bat with arg wellDoneMS from a cygwin bash script, instead of just cmdFUBAR.bat wellDoneMS, you do cygstart -w launcher.bat cmdFUBAR.bar wellDoneMS. Of course you have to put launcher.bat into your path, or call it with full path.
The answer given by #ruslo is very useful and works just fine, but there a redundancy in it: the start "clean shell" /I does nothing here :-) It is the explorer piece that gets rid of the current cygwin env, not start /i! Try omitting it from the last line of the script, like so:
cmd /C "${EXPLORER_WINPATH}" "${SCRIPT_WINPATH}"
Same result - "pristine path"!
Perusing the help for start, it says: The new environment will be the original environment passed to the cmd.exe and not the current environment. Well, but the environment passed to cmd in this case is that of cygwin, so start /i will do nothing useful here, as one can see by typing:
cmd /c start /i cmd /c "path && pause"
The path is still that of cygwin, not the windows system!
=== In the build.bat script...
SET "DOS_FIND_EXE=%SYSTEMROOT%\System32\find.exe"
...
dir | "%DOS_FIND_EXE%" "target"
if errorlevel 1 goto no_target_dir
...

How do I find a string in text files and print the matches in a Windows batch file?

Is there a way to search for a defined string within several thousand text files and to print the names of the files that have a match?
findstr /M "searched string" *.txt > matchingFiles.out
From findstr /? documentation:
/M Prints only the filename if a file contains a match.
Not sure which OS platform to be answered for,
But Linux/Unix systems can have the following command used for this purpose.
find . -name '*.ext' -type f | xargs fgrep 'pattern to be searched'
This will search the files starting from current directory and check for the 'pattern to be searched'. The file name pattern with wildcard can be applied where I have used '*.ext'

Recursively moving files to root folder

I am more used to using unix shell than CMD, and I am not really sure how to get this to work. I have a directory with several other sub-directories that contain .xml files. I would like to move all the files recursively to the root directory. I know with unix this done like so:
find FOLDERPATH -type f -name '*.xml' -exec mv -i {} FOLDERPATH \;
Yet I can't seem to find something that will work in the same way. XCOPY looked promising, but it doesn't copy only the folders, it copies the whole structure, thus I get these sub-directories that I don't want again. Has anyone got any other suggestions?
This will work from the CMD prompt. Run it in the folder you want the files to be moved to, and it will process the sub-directories in that folder.
It does not provide a mechanism to handle filename collisions elegantly.
for /R /D %f in (*) do move "%f\*.xml" .
and this will work in a batch file.
#echo off
for /R /D %%f in (*) do move "%%f\*.xml" .
Try this:
set FOLDERPATH=...
for /R "%FOLDERPATH%" %%f in (*.xml) do move "%%~ff" "%FOLDERPATH%"

Delete some files using batch files

I must create batch file to delete files of a directory which names first symbols are "a". How can I do it?
If you're using Windows try this (assuming *full_path* is directory you want to delete in):
#echo off
DEL /Q full_path\a*.*
or if you want to delete files from that dir and in its subdir, try this:
#echo ff
DEL /Q /S full_path\a*.*
If you're using Linux (or similar), try this:
rm -f full_path/a*
or
rm -rf full_path/a*
If you are using something like Linux or OSX or Cygwin try
find . -name "a*" -delete
If you want to remove whole directories
find . -name "a*" | xargs -n 5 rm -r

How to find element(s) which are not present in the clearcase recursively?

How to find element(s) which are not present in the clearcase recursively , let us say from project root folder i want to know the list of files which are not part of clearcase.
Can some one help me on this?
First you need to find them through the command line interface, in a DOS session for instance:
for /F "usebackq delims=" %i in (`cleartool ls -r -nxn ^| find /V "Rule:" ^| find /V "hijacked" ^| find /V "eclipsed"`) do #echo "%i"
(See the discussion on how to find private files in this SO answer)
Then, you can add this command to the ClearCase menu entries from the Windows explorer, follwing the process described in the ten ben scripts article.
See also this SO question and that one for example of menu entries commands (not related to your question, but mentioning a few tip about the commands)
If it's a snapshot view, then from the project root use:
cleartool ls -view -r
If it's a dynamic view, then from anywhere use:
cleartool lsp
Usually Clearcase plugin (with RSA I am sure it does) gives an option "Find Resources to add to source control"..
This will list all files which are not in ClearCase repository..
Hope this helped.. :-)

Resources