Page 1 of 1

"No valid files found" return screen from batch script (???)

Posted: 16 Jun 2010 15:45
by aefavant
Hi everyone!

So, I wrote a script to encrypt whole directories but came across a result that I can't understand.
it does a simple FOR /R %%v in (*.*) DO bcrypt %%~fv < pwd-file

For some reason that works from commandline level. great! BUT what I had been trying is to call this script from another one is the following manner:

I added a script to the Windows SendTo folder so I can click on a bunch of files and run the crypt for all of them at once.

Code: Select all

SET LOCFOLDER=%~dp1
CD %LOCFOLDER%

that bit of code guarantees I work only with the files inside the folder I right-clicked

THEN I go on calling the bat that actually encrypts by opening a new command window inside the aim folder:

Code: Select all

@cmd 


Once the new cmd pops up, I type "encrypt" and the program runs exactly the same way it does during the execution in which it works, but prints "No valid files found" and does not complete encryption...

What does this "No valid files found" mean??

cheers!

Re: "No valid files found" return screen from batch script (

Posted: 16 Jun 2010 16:12
by aGerman
If you right clicked a folder you will have the parent folder name in %~dp1. In this case you have to work with %1.

Regards
aGerman

Re: "No valid files found" return screen from batch script (

Posted: 17 Jun 2010 06:22
by aefavant
Hi aGerman!

So, this piece of code DOES work.

Code: Select all

@echo off

:: ********************************
:: Sets folder to act upon
:: ********************************

SET LOCFOLDER=%~dp1
CD %LOCFOLDER%


:: **********prog routine**********
:: ******************************
@cls

for /R %%v in (*.*) do echo %~f1

pause    ::just to be able to check the output



In this case I tell the bat to echo every file name inside the directory I right-clicked on. If I change to ~dp1 or %1 it won't work.
There is a problem though,,,, it acts on EVERY file inside the folder and not on only those I clicked! So my FOR %v in (*) is not working the way I wanted.
At first it would not be a problem because I want ot encrypt whole folders, but in case I wanted to encrypt only 100 files in a folder of 300, it would be a problem!

My doubt is why it lists the files right but when I use the very same FOR loop to run my bcrypt exe it doesn't recognise any files!
I've been trying so many different combinations of code and still no good...

About using the "%1" you mentioned, can you give my an example in that code? What if %1 represents more than 9 files??

Thanks for your help!


Nice football skills Germany is bringing this World Cup by the way. Too bad for England and Brazil (my two favs :)

Re: "No valid files found" return screen from batch script (

Posted: 17 Jun 2010 13:16
by aGerman
Hmm.
Say you have a folder C:\data\somewhere
If you rightclick the folder "somewhere" and "send to" your Batchfile, then you would find
C:\data\somewhere in %1 or
C:\data\ in %~dp1

If you would rightclick "file.ext" in C:\data\somewhere and"send to" your Batchfile, then you would find
C:\data\somewhere\file.ext in %1 or
C:\data\somewhere\ in %~dp1

Thats the difference.

Btw: You should write that single line
cd /d "%~dp1"
to make sure that you would also change the drive (if needed) and that you would change to a path with spaces (if needed).

The next interresting things are:
- Where did you place bcrypt.exe (Is it needed to write the entire path? think about you have changed the working directory by the CD command)
- The same question for your pwd-file

[OT]
Brazil was one of my favorits too. And Germany, hmm... so many times they had a good start ... :wink: Some good players are injured. But jeah, I have to believe in the German team! :P
[/OT]

Regards
aGerman

Re: "No valid files found" return screen from batch script (

Posted: 17 Jun 2010 14:57
by aefavant
No good ;(
Same nasty message.

Ok, the :

bcrypt is in c:\windows\system32
windr is in c:\

System32 is in the path so I don't see why declare its full path. The cmd would look up in that anyway.
Windr is declared with its path in the batch "c:\windr"

It must be something stupid to fix because it should not be so hard to make such a basic script run right (disappointed)

Phew.... Still trying. I'm bound to make it work!
I can send the files if you want.

cheers

Re: "No valid files found" return screen from batch script (

Posted: 17 Jun 2010 15:38
by aGerman
OK.
We should try a different way.

If you select more than one file, then the 2nd file is in %2, the 3d in %3 etc.
But all parameters you will find in %*.

So save this as "encrypt.bat" to the "Send To" folder:

Code: Select all

@echo off &setlocal
cd /d "%~dp1"
REM just to show where you are
echo %cd%
echo.
for %%a in (%*) do (
  REM to show which file is in process
  echo %%a
  bcrypt.exe %%a<"C:\somewhere\pwd-file.ext"
)
pause


Change the path of your pwd-file.

Now select some files, "send to" encrypt.bat and let me know what happens.

Regards
aGerman

Re: "No valid files found" return screen from batch script (

Posted: 18 Jun 2010 08:39
by aefavant
Well, guess what - it worked :0

Thanks. I had no idea %* would work for more than 9 arguments.

There is although a limitation. If I click on more than 26 files at the same time (27 or more) it will bring an error message like "Windows cannot access the device, file or folder that you specified. Maybe you do not have permission to acces the item."

Weird! Up to 26 files it works perfectly!

The "final" code below. It sets the key on-the-fly and then wipes it a few times to make sure no-one will ever get it.
The full script I wrote to run with Open Bcrypt uses lha, 7zip and rar to compress the files, and then encrypts the bundle. I think a solution like that might be used. But in this case, the same problem would happen... too many files...

Code: Select all

::A backup utility that safely encrypts and erases data on folders using Blowfish
:: algorithm 448bits hashed key.
::

@echo off &setlocal

:: ********************************
:: Prompt for ophrase
:: ********************************

:input
@set mypass=
@set /P mypass=Type input, no less than 8 characters:
if "%mypass%"=="" goto input

:: removes pwd file / creates pwd in it.
@erase /A c:\windr
@echo %mypass%>>c:\windr
@echo %mypass%>>c:\windr


:: ********************************
:: Sets folder to act on
:: ********************************

cd /d "%~dp1"
echo %cd%
echo.

:: **********Crypt routine**********
:: ******************************

for %%a in (%*) do (
echo %%a 
bcrypt.exe %%a<"C:\windr"
)


:: *************************************
:: clears up pwd input var & file - wipes'em clean thrice
:: *************************************

set

mypass=**************************************************************

*******************
echo %mypass%>c:\windr
echo %mypass%>>c:\windr
set mypass=I765YByuYyyg458BJ6Uhhu786HOooj7
erase /A c:\windr
echo %mypass%>c:\windr
echo %mypass%>>c:\windr
set mypass=


::@erase c:\windr

::clear screen 2x
::@CLS
::@cls
::@exit



cheers1

Re: "No valid files found" return screen from batch script (

Posted: 18 Jun 2010 14:37
by avery_larry
I would guess that it's not too many files, but rather too many characters in the for loop:

for %%a in (%*) do

if the %* is reeeeaaaaly long I'm pretty sure it'll choke. In fact, it's probably a cmd limitation, which means you probably have a character limit for the command line itself (which means we could fix the for loop using the shift command, but the command line probably still has a limit).

Re: "No valid files found" return screen from batch script (

Posted: 18 Jun 2010 15:37
by aGerman
Yes, afaik it's a limit of (less than) 2^10 characters.
So indeed shift could solve it.
Try something like that:

Code: Select all

@echo off &setlocal
:loop
  if "%~1"=="" goto end
  echo %1
  bcrypt.exe %1<"C:\windr"
  shift
goto loop

:end
pause



Regards
aGerman

[Edit: %%a replaced by %1 /]

Re: "No valid files found" return screen from batch script (

Posted: 19 Jun 2010 04:51
by amel27
this code work for me...
but at the time of encode/decode added 1 trailing space to passphrase

Code: Select all

@echo off

:in_pass
set /p pass=Type password, no less than 8 characters:
if "%pass:~7,1%"=="" goto in_pass

(if exist "%~f1\" (set flag=y& pushd "%~f1") else pushd "%~dp1")||(pause& exit /b 1)
if defined flag goto process

:in_flag
set /p flag=Process all files in this folder? [y/n]:
if /i not "%flag%"=="y" if /i not "%flag%"=="n" goto in_flag

:process
if /i %flag%==y for %%a in (*)  do (echo.%pass%& echo.%pass%)|bcrypt.exe "%%a"2>Nul
if /i %flag%==n for %%a in (%*) do (echo.%pass%& echo.%pass%)|bcrypt.exe "%%a"2>Nul

Re: "No valid files found" return screen from batch script (

Posted: 21 Jun 2010 13:59
by aefavant
Hi mates, sorry it took me a couple of days to test and reply to your efforts to help me.

Avery_larry and aGerman:

You were right about the length of characters, and not the number of files. The new proposed code + the shift worked for up to 93 files at once. Great change form 23 :) (still digesting the chances and trying to learn with it)
aGerman, I also substituted the FOR case by you code, it worked the same way. It now seems to me that I don't need a for loop/while/do until etc to many operation, since you gave me a solution using only an IF statement!


And now we get to amel27's code. You also used IF statements and not a single FOR!
It worked! Not only for single and multiple files but also for entire folders. I ran tests and here's what I got:
Created a folder with 1200 files inside. 700mb of jpegs (same one, copied hundreds of times) and then another folder inside with the same 12mb txt file. In total, 1.2Gb of data :)
I tested from the inside out and worked for en/decryption, so I set out to try the parant folder with a single right-click.
The batch did the trick successfully in less than 2minutes. 1200 files, 1.2 Gb of data.
The decryption took a few seconds less than the encryption - I give that the need to enter only once the password.
amel27 I was hoping you could provide me with a few comments on your code so I could learn more. Great show.

Thank you very much guys! Great work.

Re: "No valid files found" return screen from batch script (

Posted: 22 Jun 2010 01:22
by amel27
aefavant wrote:amel27 I was hoping you could provide me with a few comments on your code so I could learn more
sorry, my English more bad than my Bat lang... ok, I'l try

Code: Select all

@echo off

:: prompt/set passphrase, check length

:in_pass
set /p pass=Type password, no less than 8 characters:
if "%pass:~7,1%"=="" goto in_pass

:: check selected object: folder or file
:: set variable "flag" (process all content) to "y" for folder
:: make dir of selected file/folder as home dir
:: exit if error occured

(if exist "%~f1\" (set flag=y& pushd "%~f1") else pushd "%~dp1")||(pause& exit /b 1)

:: skip additional prompt for folder, goto crypt

if defined flag goto process

:: prompt/set "flag" (process all content) if select file(s)

:in_flag
set /p flag=Process all files in this folder? [y/n]:
if /i not "%flag%"=="y" if /i not "%flag%"=="n" goto in_flag

:: encrypt/decrypt routine file by file

:: if flag "y" - process all files in home dir
:: if flag "n" - process only selected files (get from command line)

:process
if /i %flag%==y for %%a in (*)  do (echo.%pass%& echo.%pass%)|bcrypt.exe "%%a"2>Nul
if /i %flag%==n for %%a in (%*) do (echo.%pass%& echo.%pass%)|bcrypt.exe "%%a"2>Nul

Re: "No valid files found" return screen from batch script (

Posted: 22 Jun 2010 12:07
by aefavant
And what does the "2>Nul" do exactly?
I understand ">nul" will print the outputs to NUL, but together with 2....


Well, I modified a bit your code to take care of the trailing characters (I echo the pass to a c: file instead) and also added "/R" to the FOR loop in order to recursively encrypt folders AND subfolders.

Re: "No valid files found" return screen from batch script (

Posted: 22 Jun 2010 13:41
by aGerman
The command line distinguishes two types of output streams:
StdOut (1) for regular output
StdErr (2) for error output

>nul redirects the StdOut to NUL. But error messages would be shown on the screen. You need 2>nul to redirect error messages to NUL.
You could use the combination
>nul 2>&1
to redirect both types.

Regards
aGerman

Re: "No valid files found" return screen from batch script (

Posted: 22 Jun 2010 17:57
by amel27
in my test bcrypt.exe prompt for pass via error, not standard stream