2

I am using GitHub Desktop on Windows and am trying to create a batch file that will execute every hour to auto commit and push. I also want it to add all files in the repository and exclude those bigger than 99Mb in the .gitignore file.

Here is what i managed to do till now:

cd C:\Users\XXX\Desktop\myRepo
forfiles /s /c "cmd /q /c if @fsize GTR 12976128 echo @relpath" >> .gitignore
git add --all
git commit -m "autoCommit %date:~-4%%date:~3,2%%date:~0,2%.%time:~0,2%%time:~3,2%%time:~6,2%"
git push
exit

The problem is, the written path to the big files have to be manipulated to look like this:

".\myFile.mkv" (Wrong)
myFile.mkv (Right)

Or

".\myFolder\myFile.mkv"
myFolder/myFile.mkv

So i tried to manipulate it with this example code:

set str=".\myFolder\myFile.mkv"
set str=%str:~3,-1%
set str=%str:\=/%
echo.%str%

Which delivers myFolder/myFile.mkv

So i implemented this code in the original one:

cd C:\Users\XXX\Desktop\myRepo
forfiles /s /c "cmd /q /c if @fsize GTR 12976128 set str=@relpath set str=%str:~3,-1% set str=%str:\=/% echo.%str%" >> .gitignore
git add --all
git commit -m "autoCommit %date:~-4%%date:~3,2%%date:~0,2%.%time:~0,2%%time:~3,2%%time:~6,2%"
git push
exit

But it doesn't work. I'm not good with Batch scripting so maybe someone knows how to make this work?

Toto
  • 89,455
  • 62
  • 89
  • 125
Aziz zeGeek
  • 107
  • 1
  • 2
  • 10
  • To run a task every hour, Windows has a Task Scheduler. – Compo May 08 '21 at 22:13
  • If you're trying to use Git as some sort of backup or file storage solution, it's not a good choice for that, and you should choose a different solution. – bk2204 May 08 '21 at 22:29
  • @bk2204 what would you suggest then? – Aziz zeGeek May 08 '21 at 22:51
  • What is your goal? You could use a cloud bucket if you're storing general files, or a cloud syncing service (e.g. Dropbox) for more document-oriented workflows. There are a variety of backup solutions; I use tarsnap, but that's mostly for Unix systems, but there are good options for every OS. – bk2204 May 09 '21 at 00:33

2 Answers2

2

Here's an example related to your specific issue, and which uses your chosen methodology:

%SystemRoot%\System32\forfiles.exe /S /C "%SystemRoot%\System32\cmd.exe /D/V/C If @IsDir==FALSE If @FSize Gtr 12976128 Set \"}=@RelPath\"&Set \"}=!}:\=/!\"&Echo !}:~3,-1!">".gitignore"

Any other issues are outside of the scope of your question which should be limited to one issue only!

Compo
  • 36,585
  • 5
  • 27
  • 39
2

For the sake of completeness, I would like to mention the alternative approach, using C:\Program Files\Git\bin\bash.exe which comes with your Git for Windows:

pushd "%userprofile%\Desktop\myRepo"
echo.>.gitignore
bash -c "command find . -type f -size +99M >> .gitignore"
bash -c "sed -i "s,^\./,," .gitignore" 
git add --all
git commit -m "autoCommit %date:~-4%%date:~3,2%%date:~0,2%.%time:~0,2%%time:~3,2%%time:~6,2%"
git push
popd
exit

That will reset, then populate .gitignore with the large files, using a compatible linux-like path

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the feedback. But i get an error saying: find: invalid number '99M' – Aziz zeGeek May 08 '21 at 23:29
  • @AzizzeGeek Strange, I just tested it, and it does work without error. – VonC May 08 '21 at 23:32
  • I would add that git provides `git check-ignore` which will tell you if a file is ignored. This could be helpful in this solution to ensure you do not add duplicates to the `.gitignore` file, while also allowing you to ignore other files. – Kraigolas May 09 '21 at 00:32
  • 1
    @Kraigolas Duplicate? I do reset (empty) .gitignore for that reason, to not introduce duplicate. Otherwise, you are correct, git check-ignore -v is awaesome, and I mentioned it initially in https://stackoverflow.com/a/26908774/6309 – VonC May 09 '21 at 00:36
  • @VonC You're correct I did see that you are clearing the .gitignore, which works in this case. But if it happens to be that OP wants to ignore both large files *and* some other files, clearing the .gitignore will not suffice. I figured my comment might help a future reader or OP. – Kraigolas May 09 '21 at 00:38
  • @Kraigolas Then I would save those constants in a separate `.gitignore.keep` file, and start with `echo .gitignore.keep>.gitignore` (resetting `.gitignopre` to the proper content), and proceed as illustrated in the answer: much simpler. – VonC May 09 '21 at 00:39
  • @Vonc So i found a solution for that error by putting +202752 (which corresponds to around 99 Mb) But again i need to format the path from ./myFile.mkv to myFile.mkv – Aziz zeGeek May 09 '21 at 00:40
  • @AzizzeGeek Interesting. I don't have to do that, but glad it is working for you. – VonC May 09 '21 at 00:41
  • @VonC any idea how to format the path from ./myFile.mkv to myFile.mkv? – Aziz zeGeek May 09 '21 at 00:43
  • @AzizzeGeek Sure. I have edited the answer accordingly. – VonC May 09 '21 at 00:48
  • Thanks, but that doesnt work for folders.. Example: ./myFolder/myFile.mkv should be myFolder/myFile.mkv . Here it's myFoldemyFile.mkv so it removes the last char of Folder's name (r) and it removes the slash. Any idea how to solve it? – Aziz zeGeek May 09 '21 at 01:01
  • @AzizzeGeek I forgot to escape the dot. I have edited the answer. – VonC May 09 '21 at 01:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232132/discussion-between-aziz-zegeek-and-vonc). – Aziz zeGeek May 09 '21 at 01:23
  • 1
    @Gerhard Thank you. I have edited the answer accordingly. – VonC May 09 '21 at 08:10
  • I'm kind of suprised that this method would be considered, because if you were going to use bash.exe, then do it all in bash. A batch file shouldn't really be necessary, especially as bash could more easily be used to add the date and time to the commit message instead of using that awful locale dependent cmd.exe method! – Compo May 09 '21 at 10:09
  • @Compo Agreed. It was a compromised to start with the OP script, but included Linux commands just for the `.gitignore` part. – VonC May 09 '21 at 10:46